From 8a5ab4fe761cf843548fef51e70b083e4e7d5a7b Mon Sep 17 00:00:00 2001 From: Henry Camacho Date: Mon, 13 Feb 2017 20:36:01 -0600 Subject: [PATCH] Data Written to the Channel - added support for ConnByDomain which looks up existing WSS and registered domain - passed connection table to External Listener - on request, obtained hostname and map to domain, split the remote address and port (will go into an table eventually) - look up the domain and find the WSS connection - packed up the frame. - sent down the channel. --- rvpn/connection/connection.go | 5 ++++ rvpn/connection/connection_table.go | 6 +++++ rvpn/external/listener_webrequest.go | 36 +++++++++++++++++++++++++--- rvpn/packer/packer_data.go | 5 ++++ rvpn/rvpnmain/run.go | 2 +- 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/rvpn/connection/connection.go b/rvpn/connection/connection.go index 0a2fd15..33aaea9 100755 --- a/rvpn/connection/connection.go +++ b/rvpn/connection/connection.go @@ -51,6 +51,11 @@ func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress s return } +//SendCh -- property to sending channel +func (c *Connection) SendCh() chan []byte { + return c.send +} + func (c *Connection) addIn(num int64) { c.bytesIn = c.bytesIn + num } diff --git a/rvpn/connection/connection_table.go b/rvpn/connection/connection_table.go index 0b1449f..f85ad08 100755 --- a/rvpn/connection/connection_table.go +++ b/rvpn/connection/connection_table.go @@ -29,6 +29,12 @@ func NewTable() (p *Table) { return } +//ConnByDomain -- Obtains a connection from a domain announcement. +func (c *Table) ConnByDomain(domain string) (conn *Connection, ok bool) { + conn, ok = c.domains[domain] + return +} + //Run -- Execute func (c *Table) Run() { loginfo.Println("ConnectionTable starting") diff --git a/rvpn/external/listener_webrequest.go b/rvpn/external/listener_webrequest.go index 61ab88c..62440d1 100644 --- a/rvpn/external/listener_webrequest.go +++ b/rvpn/external/listener_webrequest.go @@ -5,11 +5,16 @@ import ( "net" "net/http" "net/http/httputil" + "strconv" + + "strings" + + "git.daplie.com/Daplie/go-rvpn-server/rvpn/connection" + "git.daplie.com/Daplie/go-rvpn-server/rvpn/packer" ) //LaunchWebRequestExternalListener - starts up extern http listeners, gets request and prep's to hand it off inside. -func LaunchWebRequestExternalListener(serverBinding *string) { - +func LaunchWebRequestExternalListener(serverBinding *string, connectionTable *connection.Table) { loginfo.Println("starting WebRequestExternal Listener ", *serverBinding) mux := http.NewServeMux() @@ -25,8 +30,33 @@ func LaunchWebRequestExternalListener(serverBinding *string) { loginfo.Printf("%q", dump) } - } + hostname := r.Host + if strings.Contains(hostname, ":") { + arr := strings.Split(hostname, ":") + hostname = arr[0] + } + + remoteSplit := strings.Split(r.RemoteAddr, ":") + rAddr := remoteSplit[0] + rPort := remoteSplit[1] + + if conn, ok := connectionTable.ConnByDomain(hostname); !ok { + //matching connection can not be found based on ConnByDomain + loginfo.Println("unable to match ", hostname, " to an existing connection") + http.Error(w, "Domain not supported", http.StatusBadRequest) + + } else { + loginfo.Println(conn, rAddr, rPort) + p := packer.NewPacker() + p.Header.SetAddress("127.0.0.2") + p.Header.Port, err = strconv.Atoi(rPort) + p.Data.AppendBytes(dump) + buf := p.PackV1() + + conn.SendCh() <- buf.Bytes() + } + } }) s := &http.Server{ Addr: *serverBinding, diff --git a/rvpn/packer/packer_data.go b/rvpn/packer/packer_data.go index dde1942..092dd8f 100644 --- a/rvpn/packer/packer_data.go +++ b/rvpn/packer/packer_data.go @@ -17,3 +17,8 @@ func (p packerData) AppendString(dataString string) (n int, err error) { n, err = p.buffer.WriteString(dataString) return } + +func (p packerData) AppendBytes(dataBytes []byte) (n int, err error) { + n, err = p.buffer.Write(dataBytes) + return +} diff --git a/rvpn/rvpnmain/run.go b/rvpn/rvpnmain/run.go index 1f6989c..438d12e 100644 --- a/rvpn/rvpnmain/run.go +++ b/rvpn/rvpnmain/run.go @@ -56,7 +56,7 @@ func Run() { connectionTable = connection.NewTable() go connectionTable.Run() go client.LaunchClientListener(connectionTable, &secretKey, &argServerBinding) - go external.LaunchWebRequestExternalListener(&argServerExternalBinding) + go external.LaunchWebRequestExternalListener(&argServerExternalBinding, connectionTable) err := admin.LaunchAdminListener(&argServerAdminBinding) if err != nil {