added NAT traversal

This commit is contained in:
2023-02-19 23:11:35 +00:00
parent 2bc03b8048
commit b7cfdba90d
7 changed files with 1086 additions and 231 deletions

View File

@@ -25,6 +25,8 @@ type Config struct {
IPAddress string
ScriptToExecute string
SSHPassword string
NATEscapeServerPort string
NATEscapeBarrierPort string
}
// Exists reports whether the named file or directory exists.
@@ -125,3 +127,42 @@ func ConfigInit() (*Config, error) {
return &config, nil
}
func (c *Config) WriteConfig() error {
//Getting Current Directory from environment variable
curDir := os.Getenv("REMOTEGAMING")
//Setting current directory to default path
defaultPath = curDir + "/"
//Setting default paths for the config file
defaults["SystemUsername"] = c.SystemUsername
defaults["BarrierHostName"] = c.BarrierHostName
defaults["Rooms"] = c.Rooms
defaults["IPAddress"] = c.IPAddress
defaults["ScriptToExecute"] = c.ScriptToExecute
defaults["SSHPassword"] = c.SSHPassword
defaults["NATEscapeServerPort"] = c.NATEscapeServerPort
defaults["NATEscapeBarrierPort"] = c.NATEscapeBarrierPort
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
//Create rooms.json file
os.Create(defaultPath + "room.json")
// If the config file exists remove and make a new one
if fileExists(defaultPath + "config.json") {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return err
}
}
//Calling configuration file
_, err := ConfigInit()
if err != nil {
return err
}
return nil
}

165
core/escape-NAT.go Normal file
View File

@@ -0,0 +1,165 @@
package core
import (
"github.com/fatedier/frp/client"
"github.com/fatedier/frp/pkg/config"
"github.com/phayes/freeport"
"io/ioutil"
"net/http"
"strconv"
"time"
)
type Server struct {
address string
port int
}
// Client This struct stores
// client information with server
// proxy connected
type Client struct {
Name string
Server *Server
ClientMappings []ClientMapping
}
// ClientMapping Stores client mapping ports
// to proxy server
type ClientMapping struct {
LocalIP string
LocalPort int
RemotePort int
}
// StartFRPClientForServer Starts Server using FRP server
// returns back a port
func StartFRPClientForServer(ipaddress string, serverport string, localport string) (string, error) {
// Setup server information
var s Server
s.address = ipaddress
// convert serverport to int
portInt, err := strconv.Atoi(serverport)
if err != nil {
return "", err
}
s.port = portInt
// Setup client information
var c Client
c.Name = "ServerPort"
c.Server = &s
// converts localport to int
portInt, err = strconv.Atoi(localport)
if err != nil {
return "", err
}
//random serverport
//randPort := rangeIn(10000, 99999)
OpenPorts, err := freeport.GetFreePorts(1)
if err != nil {
return "", err
}
c.ClientMappings = []ClientMapping{
{
LocalIP: "localhost",
LocalPort: portInt,
RemotePort: OpenPorts[0],
},
}
// Start client server
go c.StartFRPClient()
return strconv.Itoa(OpenPorts[0]), nil
}
// StartFRPClient Starts FRP client
func (c *Client) StartFRPClient() error {
cfg := config.GetDefaultClientConf()
var proxyConfs map[string]config.ProxyConf
var visitorCfgs map[string]config.VisitorConf
proxyConfs = make(map[string]config.ProxyConf)
cfg.ServerAddr = c.Server.address
cfg.ServerPort = c.Server.port
for i, _ := range c.ClientMappings {
var tcpcnf config.TCPProxyConf
tcpcnf.LocalIP = c.ClientMappings[i].LocalIP
tcpcnf.LocalPort = c.ClientMappings[i].LocalPort
tcpcnf.RemotePort = c.ClientMappings[i].RemotePort
proxyConfs[tcpcnf.ProxyName] = &tcpcnf
}
cli, err := client.NewService(cfg, proxyConfs, visitorCfgs, "")
if err != nil {
return err
}
cli.Run()
return nil
}
func GetFRPServerPort(host string) (string, error) {
resp, err := http.Get(host + "/FRPPort")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// EscapeNAT Func to escape NAT
// - 1 port for server
// - 2 port for barrierKVM
func EscapeNAT(Port string) (ServerPort string, barrierKVMport string, err error) {
// Get free port from P2PRC server node
serverPort, err := GetFRPServerPort("http://64.227.168.102:8088")
if err != nil {
return
}
time.Sleep(1 * time.Second)
// port for the remote gameplay server
ServerPort, err = StartFRPClientForServer("64.227.168.102", serverPort, Port)
if err != nil {
return
}
// Get free port from P2PRC server node
serverPort, err = GetFRPServerPort("http://64.227.168.102:8088")
if err != nil {
return
}
time.Sleep(1 * time.Second)
//port for the barrierKVM server
barrierKVMport, err = StartFRPClientForServer("64.227.168.102", serverPort, "24800")
if err != nil {
return
}
return
}

16
core/escape-NAT_test.go Normal file
View File

@@ -0,0 +1,16 @@
package core
import (
"fmt"
"testing"
)
func TestEscapeNAT(t *testing.T) {
nat, s, err := EscapeNAT("8090")
if err != nil {
fmt.Println(err)
t.Fail()
}
fmt.Println(nat)
fmt.Println(s)
}

View File

@@ -149,6 +149,9 @@ function initUI() {
LaplaceVar.ui.SSHPassword = document.getElementById('SSHPassword')
LaplaceVar.headless = { result :"0"};
//getting server hostname
getServerIPandHostname()
LaplaceVar.ui.joinForm.onsubmit = async e => {
e.preventDefault();
LaplaceVar.roomID = LaplaceVar.ui.inputRoomID.value;
@@ -199,9 +202,6 @@ function initUI() {
return startStream(mediaOption, pcOption);
}
//getting server hostname
getServerIPandHostname()
//getting SSHPassword
getSSHPassword()

4
go.mod
View File

@@ -3,6 +3,8 @@ module github.com/Akilan1999/remotegameplay
go 1.16
require (
github.com/gorilla/websocket v1.4.2
github.com/fatedier/frp v0.47.0
github.com/gorilla/websocket v1.5.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/spf13/viper v1.8.1
)

635
go.sum

File diff suppressed because it is too large Load Diff

30
main.go
View File

@@ -14,7 +14,7 @@ import (
)
func main() {
addr := flag.String("addr", "0.0.0.0:443", "Listen address")
addr := flag.String("addr", "localhost", "Listen address")
port := flag.String("port", "8888", "port for running the server")
tls := flag.Bool("tls", false, "Use TLS")
setconfig := flag.Bool("setconfig", false, "Generates a config file")
@@ -111,17 +111,37 @@ func main() {
return
}
// running implementation to escape NAT
Server, barrireKVM, err := core.EscapeNAT(*port)
if err != nil {
log.Fatalln(err)
}
Config, err := config.ConfigInit()
if err != nil {
log.Fatalln(err)
}
Config.IPAddress = "64.227.168.102"
Config.NATEscapeServerPort = Server
Config.NATEscapeBarrierPort = barrireKVM
err = Config.WriteConfig()
if err != nil {
log.Fatalln(err)
}
if *tls {
log.Println("Listening on TLS:", *addr)
if err := http.ListenAndServeTLS(*addr, *certFile, *keyFile, server); err != nil {
log.Println("Listening on TLS:", *addr+":"+*port)
if err := http.ListenAndServeTLS(*addr+":"+*port, *certFile, *keyFile, server); err != nil {
log.Fatalln(err)
}
} else {
log.Println("Listening:", *addr)
if err := http.ListenAndServe(*addr, server); err != nil {
log.Println("Listening:", *addr+":"+*port)
if err := http.ListenAndServe(*addr+":"+*port, server); err != nil {
log.Fatalln(err)
}
}
}
// PrettyPrint print the contents of the obj (