removed map check

This commit is contained in:
2024-08-23 03:45:01 +01:00
parent 63d079f71d
commit 2c8ef44bd8

View File

@@ -2,37 +2,37 @@
package server
import (
"errors"
"fmt"
"github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/gin-gonic/gin"
"log"
"net/http"
"net/http/httputil"
"net/url"
"errors"
"fmt"
"github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/gin-gonic/gin"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
var (
//RunPort = 2002 // The server port to run on
//ReverseServerAddr = fmt.Sprint("0.0.0.0:", RunPort) // this is our reverse server ip address
//InsideProxyHostname = fmt.Sprint("proxy:", RunPort) // Requests from private network
//OutsideProxyHostname = fmt.Sprint("registration.localhost:", RunPort) // Requests from public network
KnownAddresses = map[string]string{} // Known Addresses
//RunPort = 2002 // The server port to run on
//ReverseServerAddr = fmt.Sprint("0.0.0.0:", RunPort) // this is our reverse server ip address
//InsideProxyHostname = fmt.Sprint("proxy:", RunPort) // Requests from private network
//OutsideProxyHostname = fmt.Sprint("registration.localhost:", RunPort) // Requests from public network
KnownAddresses = map[string]string{} // Known Addresses
)
type RegistrationRequest struct {
OutsideHost string // Outside host address. The hostname or IP on the public network. For example foo.bar.com or foo.localhost
InsideHost string // Inside host address. The hostname or IP on the internal network. For example 192.168.10.5
OutsideHost string // Outside host address. The hostname or IP on the public network. For example foo.bar.com or foo.localhost
InsideHost string // Inside host address. The hostname or IP on the internal network. For example 192.168.10.5
}
// This function checks if TLS was enabled on the request
// and translates it to the proper scheme (http or https)
func GetScheme(c *gin.Context) string {
if c.Request.TLS != nil {
return "https"
} else {
return "http"
}
if c.Request.TLS != nil {
return "https"
} else {
return "http"
}
}
// IsRegistrationRequest checks if an incoming request is meant to register
@@ -65,13 +65,13 @@ func GetScheme(c *gin.Context) string {
// SaveRegistration Creates registration of proxy node
func SaveRegistration(OutsideHost string, InsideHost string) error {
_, ok := KnownAddresses[OutsideHost]
if !ok {
return errors.New("domain name provided already exists")
}
KnownAddresses[OutsideHost] = InsideHost
_, ok := KnownAddresses[OutsideHost]
if !ok {
return errors.New("domain name provided already exists")
}
KnownAddresses[OutsideHost] = InsideHost
return nil
return nil
}
// Proxy runs the actual proxy and will look at the
@@ -80,65 +80,65 @@ func SaveRegistration(OutsideHost string, InsideHost string) error {
// request
func Proxy(c *gin.Context) {
// Get if HTTP or HTTPS
scheme := GetScheme(c)
// Get if HTTP or HTTPS
scheme := GetScheme(c)
log.Println(scheme, c.Request.Host, c.Request.URL.String())
log.Println(scheme, c.Request.Host, c.Request.URL.String())
// Translate the outside hostname to the inside hostname
forwardTo, ok := KnownAddresses[c.Request.Host]
// Translate the outside hostname to the inside hostname
forwardTo, _ := KnownAddresses[c.Request.Host]
if !ok {
log.Printf("Unkown Host: %v", c.Request.Host)
c.String(400, "Unkown Host")
return
}
//if !ok {
// log.Printf("Unkown Host: %v", c.Request.Host)
// c.String(400, "Unkown Host")
// return
//}
rUrl := fmt.Sprintf("%v://%v%v", scheme, forwardTo, c.Request.URL)
rUrl := fmt.Sprintf("%v://%v%v", scheme, forwardTo, c.Request.URL)
remote, err := url.Parse(rUrl)
remote, err := url.Parse(rUrl)
if err != nil {
log.Println(err)
c.String(500, "Error Proxying Host")
return
}
if err != nil {
log.Println(err)
c.String(500, "Error Proxying Host")
return
}
log.Println("Forwarding request to", remote)
log.Println("Forwarding request to", remote)
// Forward the request to the inside remote server
// https://pkg.go.dev/net/http/httputil#NewSingleHostReverseProxy
proxy := httputil.NewSingleHostReverseProxy(remote)
// Forward the request to the inside remote server
// https://pkg.go.dev/net/http/httputil#NewSingleHostReverseProxy
proxy := httputil.NewSingleHostReverseProxy(remote)
// Director is a function which modifies
// the request into a new request to be sent
// https://pkg.go.dev/net/http/httputil#ReverseProxy
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = c.Param("path")
}
// Director is a function which modifies
// the request into a new request to be sent
// https://pkg.go.dev/net/http/httputil#ReverseProxy
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = c.Param("path")
}
proxy.ServeHTTP(c.Writer, c.Request)
proxy.ServeHTTP(c.Writer, c.Request)
}
func ProxyRun(port string) {
r := gin.Default()
r := gin.Default()
KnownAddresses = make(map[string]string)
KnownAddresses = make(map[string]string)
// Get config information
Config, err := config.ConfigInit(nil, nil)
if err != nil {
log.Printf("Error: %v", err)
}
// Get config information
Config, err := config.ConfigInit(nil, nil)
if err != nil {
log.Printf("Error: %v", err)
}
// all paths should be handled by Proxy()
r.Any("/*path", Proxy)
// all paths should be handled by Proxy()
r.Any("/*path", Proxy)
if err := r.RunTLS(fmt.Sprint("0.0.0.0:", port), Config.PemFile, Config.KeyFile); err != nil {
log.Printf("Error: %v", err)
}
if err := r.RunTLS(fmt.Sprint("0.0.0.0:", port), Config.PemFile, Config.KeyFile); err != nil {
log.Printf("Error: %v", err)
}
}