diff --git a/README.md b/README.md index c6d9611..542130c 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,12 @@ INFO: 2017/02/02 21:24:15 connection.go:113: websocket opening 127.0.0.1:55487 INFO: 2017/02/02 21:24:15 connection.go:123: access_token invalid...closing connection ``` +Connection to the External Interface. +http://127.0.0.1:8080 + +The request is dumped to stdio. This is in preparation of taking that request and sending it back to the designated WSS connection +The system needs to track the response coming back, decouple it, and place it back on the wire in the form of a response stream. Since + A Poor Man's Reverse VPN written in Go Context diff --git a/connection.go b/connection.go index 3be442a..737da0b 100755 --- a/connection.go +++ b/connection.go @@ -2,11 +2,9 @@ package main import ( "encoding/hex" - "net/http" "time" - "github.com/dgrijalva/jwt-go" "github.com/gorilla/websocket" ) @@ -105,42 +103,3 @@ func (c *Connection) sender() { } } } - -// handleConnectionWebSocket handles websocket requests from the peer. -func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.ResponseWriter, r *http.Request, admin bool) { - loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host) - - tokenString := r.URL.Query().Get("access_token") - result, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { - return []byte(secretKey), nil - }) - - if err != nil || !result.Valid { - w.WriteHeader(http.StatusForbidden) - w.Write([]byte("Not Authorized")) - loginfo.Println("access_token invalid...closing connection") - return - } - - loginfo.Println("access_token valid") - - claims := result.Claims.(jwt.MapClaims) - loginfo.Println("processing domains", claims["domains"]) - - if admin == true { - loginfo.Println("Recognized Admin connection, waiting authentication") - } else { - loginfo.Println("Recognized connection, waiting authentication") - } - - conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - loginfo.Println("WebSocket upgrade failed", err) - return - } - connection := &Connection{connectionTable: connectionTable, conn: conn, send: make(chan []byte, 256), source: r.RemoteAddr, admin: admin} - connection.connectionTable.register <- connection - go connection.writer() - //go connection.sender() - connection.reader() -} diff --git a/listener_admin.go b/listener_admin.go index 517346f..e6b95c1 100644 --- a/listener_admin.go +++ b/listener_admin.go @@ -7,30 +7,34 @@ import ( //launchAdminListener - starts up http listeners and handles various URI paths func launchAdminListener() { - loginfo.Println("starting Admin Listener") + loginfo.Println("starting launchAdminListener", *argServerBinding) - http.HandleFunc("/admin", handlerServeAdminContent) + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + switch url := r.URL.Path; url { + case "/": + handleConnectionWebSocket(connectionTable, w, r, false) + //w.Header().Set("Content-Type", "text/html; charset=utf-8") + //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) - err := http.ListenAndServeTLS(*argServerAdminBinding, "certs/fullchain.pem", "certs/privkey.pem", nil) + case "/admin": + w.Header().Set("Content-Type", "text/html; charset=utf-8") + template.Must(template.ParseFiles("html/admin.html")).Execute(w, r.Host) + + default: + http.Error(w, "Not Found", 404) + + } + + }) + s := &http.Server{ + Addr: *argServerAdminBinding, + Handler: mux, + } + + err := s.ListenAndServe() if err != nil { logfatal.Println("ListenAndServe: ", err) panic(err) } } - -func handlerServeAdminContent(w http.ResponseWriter, r *http.Request) { - switch url := r.URL.Path; url { - case "/": - handleConnectionWebSocket(connectionTable, w, r, false) - //w.Header().Set("Content-Type", "text/html; charset=utf-8") - //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) - - case "/admin": - w.Header().Set("Content-Type", "text/html; charset=utf-8") - template.Must(template.ParseFiles("html/admin.html")).Execute(w, r.Host) - - default: - http.Error(w, "Not Found", 404) - - } -} diff --git a/listener_client.go b/listener_client.go index 4d18f96..b84dede 100644 --- a/listener_client.go +++ b/listener_client.go @@ -1,44 +1,76 @@ package main import ( - "html/template" "net/http" + + jwt "github.com/dgrijalva/jwt-go" ) -//launchListener - starts up http listeners and handles various URI paths +//launchClientListener - starts up http listeners and handles various URI paths func launchClientListener() { - loginfo.Println("starting Client Listener ", argServerBinding) + loginfo.Println("starting WebRequestExternal Listener ", *argServerBinding) - connectionTable = newConnectionTable() - go connectionTable.run() - http.HandleFunc("/", handlerServeContent) + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + switch url := r.URL.Path; url { + case "/": + handleConnectionWebSocket(connectionTable, w, r, false) - err := http.ListenAndServeTLS(*argServerBinding, "certs/fullchain.pem", "certs/privkey.pem", nil) + default: + http.Error(w, "Not Found", 404) + + } + + }) + + s := &http.Server{ + Addr: *argServerBinding, + Handler: mux, + } + + err := s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem") if err != nil { - logfatal.Println("ListenAndServe: ", err) + logfatal.Println("ListenAndServeTLS: ", err) panic(err) } } -func handlerServeContent(w http.ResponseWriter, r *http.Request) { - switch url := r.URL.Path; url { - case "/": - handleConnectionWebSocket(connectionTable, w, r, false) - //w.Header().Set("Content-Type", "text/html; charset=utf-8") - //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) +// handleConnectionWebSocket handles websocket requests from the peer. +func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.ResponseWriter, r *http.Request, admin bool) { + loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host) - case "/admin": - w.Header().Set("Content-Type", "text/html; charset=utf-8") - template.Must(template.ParseFiles("html/admin.html")).Execute(w, r.Host) - - case "/ws/client": - handleConnectionWebSocket(connectionTable, w, r, false) - - case "/ws/admin": - handleConnectionWebSocket(connectionTable, w, r, true) - - default: - http.Error(w, "Not Found", 404) + tokenString := r.URL.Query().Get("access_token") + result, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + return []byte(secretKey), nil + }) + if err != nil || !result.Valid { + w.WriteHeader(http.StatusForbidden) + w.Write([]byte("Not Authorized")) + loginfo.Println("access_token invalid...closing connection") + return } + + loginfo.Println("access_token valid") + + claims := result.Claims.(jwt.MapClaims) + loginfo.Println("processing domains", claims["domains"]) + + if admin == true { + loginfo.Println("Recognized Admin connection, waiting authentication") + } else { + loginfo.Println("Recognized connection, waiting authentication") + } + + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + loginfo.Println("WebSocket upgrade failed", err) + return + } + loginfo.Println("before connection table") + connection := &Connection{connectionTable: connectionTable, conn: conn, send: make(chan []byte, 256), source: r.RemoteAddr, admin: admin} + connection.connectionTable.register <- connection + go connection.writer() + //go connection.sender() + connection.reader() } diff --git a/listener_webrequest.go b/listener_webrequest.go new file mode 100644 index 0000000..b1acef3 --- /dev/null +++ b/listener_webrequest.go @@ -0,0 +1,37 @@ +package main + +import "net/http" +import "net/http/httputil" + +//launchWebRequestListener - starts up extern http listeners, gets request and prep's to hand it off inside. +func launchWebRequestExternalListener() { + + loginfo.Println("starting WebRequestExternal Listener ", *argServerExternalBinding) + + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + switch url := r.URL.Path; url { + default: + loginfo.Println("handlerWebRequestExternal") + + dump, err := httputil.DumpRequest(r, true) + if err != nil { + loginfo.Println(err) + } else { + loginfo.Printf("%q", dump) + } + + } + + }) + s := &http.Server{ + Addr: *argServerExternalBinding, + Handler: mux, + } + + err := s.ListenAndServe() + if err != nil { + logfatal.Println("ListenAndServe: ", err) + panic(err) + } +} diff --git a/vpn-server.go b/vpn-server.go index 8922513..8a697aa 100644 --- a/vpn-server.go +++ b/vpn-server.go @@ -26,13 +26,14 @@ const ( var ( //Info .. - loginfo *log.Logger - logfatal *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile - argServerBinding = flag.String("server-port", "127.0.0.1:8000", "server Bind listener") - argServerAdminBinding = flag.String("admin-server-port", "127.0.0.2:8000", "admin server Bind listener") - connectionTable *ConnectionTable - secretKey = "abc123" + loginfo *log.Logger + logfatal *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + argServerBinding = flag.String("server-port", "127.0.0.1:3502", "server Bind listener") + argServerAdminBinding = flag.String("admin-server-port", "127.0.0.2:8000", "admin server Bind listener") + argServerExternalBinding = flag.String("external-server-port", "127.0.0.1:8080", "external server Bind listener") + connectionTable *ConnectionTable + secretKey = "abc123" ) func logInit(infoHandle io.Writer) { @@ -49,6 +50,9 @@ func main() { loginfo.Println("startup") flag.Parse() + connectionTable = newConnectionTable() + go connectionTable.run() go launchClientListener() + go launchWebRequestExternalListener() launchAdminListener() }