From d7e01e8b407f542aaf3aeb5a0d3f3945020830fe Mon Sep 17 00:00:00 2001 From: Henry Camacho Date: Tue, 7 Mar 2017 19:34:16 -0600 Subject: [PATCH] Adjusted to remove excessive packages. Moved XLATE into generic listener. --- {rvpn/html => html}/admin.html | 0 {rvpn/html => html}/client.html | 0 main.go | 3 +- rvpn/admin/setup.go | 17 -- rvpn/client/listener_client.go | 84 --------- rvpn/client/setup.go | 17 -- rvpn/connection/setup.go | 17 -- rvpn/connectiontrack/setup.go | 17 -- rvpn/external/listener_webrequest.go | 82 --------- rvpn/external/server.go | 174 ------------------ rvpn/external/setup.go | 17 -- .../{xlate => genericlistener}/wss_mapping.go | 7 +- rvpn/instrumentation/setup.go | 17 -- rvpn/matching/protocol.go | 62 ------- rvpn/matching/setup.go | 13 -- 15 files changed, 2 insertions(+), 525 deletions(-) rename {rvpn/html => html}/admin.html (100%) rename {rvpn/html => html}/client.html (100%) delete mode 100644 rvpn/admin/setup.go delete mode 100644 rvpn/client/listener_client.go delete mode 100644 rvpn/client/setup.go delete mode 100644 rvpn/connection/setup.go delete mode 100644 rvpn/connectiontrack/setup.go delete mode 100644 rvpn/external/listener_webrequest.go delete mode 100644 rvpn/external/server.go delete mode 100644 rvpn/external/setup.go rename rvpn/{xlate => genericlistener}/wss_mapping.go (94%) delete mode 100755 rvpn/instrumentation/setup.go delete mode 100644 rvpn/matching/protocol.go delete mode 100644 rvpn/matching/setup.go diff --git a/rvpn/html/admin.html b/html/admin.html similarity index 100% rename from rvpn/html/admin.html rename to html/admin.html diff --git a/rvpn/html/client.html b/html/client.html similarity index 100% rename from rvpn/html/client.html rename to html/client.html diff --git a/main.go b/main.go index ddba4a5..33f06e3 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "context" "git.daplie.com/Daplie/go-rvpn-server/rvpn/genericlistener" - "git.daplie.com/Daplie/go-rvpn-server/rvpn/xlate" ) var ( @@ -25,7 +24,7 @@ var ( argServerExternalBinding string argDeadTime int connectionTable *genericlistener.Table - wssMapping *xlate.WssMapping + wssMapping *genericlistener.WssMapping secretKey = "abc123" ) diff --git a/rvpn/admin/setup.go b/rvpn/admin/setup.go deleted file mode 100644 index a9b231c..0000000 --- a/rvpn/admin/setup.go +++ /dev/null @@ -1,17 +0,0 @@ -package admin - -import ( - "log" - "os" -) - -var ( - loginfo *log.Logger - logdebug *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile -) - -func init() { - loginfo = log.New(os.Stdout, "INFO: admin: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: admin:", logFlags) -} diff --git a/rvpn/client/listener_client.go b/rvpn/client/listener_client.go deleted file mode 100644 index c53c346..0000000 --- a/rvpn/client/listener_client.go +++ /dev/null @@ -1,84 +0,0 @@ -package client - -import ( - "net/http" - - jwt "github.com/dgrijalva/jwt-go" - "github.com/gorilla/websocket" - - "git.daplie.com/Daplie/go-rvpn-server/rvpn/connection" -) - -//LaunchClientListener - starts up http listeners and handles various URI paths -func LaunchClientListener(connectionTable *connection.Table, secretKey *string, serverBinding *string) (err error) { - loginfo.Println("starting WebRequestExternal Listener ", *serverBinding) - - mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - switch url := r.URL.Path; url { - case "/": - handleConnectionWebSocket(connectionTable, w, r, *secretKey, false) - - default: - http.Error(w, "Not Found", 404) - } - }) - - s := &http.Server{ - Addr: *serverBinding, - Handler: mux, - } - - err = s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem") - if err != nil { - loginfo.Println("ListenAndServeTLS: ", err) - } - return -} - -// handleConnectionWebSocket handles websocket requests from the peer. -func handleConnectionWebSocket(connectionTable *connection.Table, w http.ResponseWriter, r *http.Request, secretKey string, 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("help access_token valid") - - claims := result.Claims.(jwt.MapClaims) - domains, ok := claims["domains"].([]interface{}) - - var upgrader = websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024, - } - - conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - loginfo.Println("WebSocket upgrade failed", err) - return - } - - loginfo.Println("before connection table") - - //newConnection := connection.NewConnection(connectionTable, conn, r.RemoteAddr, domains) - - newRegistration := connection.NewRegistration(conn, r.RemoteAddr, domains) - connectionTable.Register() <- newRegistration - ok = <-newRegistration.CommCh() - if !ok { - loginfo.Println("connection registration failed ", newRegistration) - return - } - - loginfo.Println("connection registration accepted ", newRegistration) -} diff --git a/rvpn/client/setup.go b/rvpn/client/setup.go deleted file mode 100644 index 5be0c5e..0000000 --- a/rvpn/client/setup.go +++ /dev/null @@ -1,17 +0,0 @@ -package client - -import ( - "log" - "os" -) - -var ( - loginfo *log.Logger - logdebug *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile -) - -func init() { - loginfo = log.New(os.Stdout, "INFO: client: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: client:", logFlags) -} \ No newline at end of file diff --git a/rvpn/connection/setup.go b/rvpn/connection/setup.go deleted file mode 100644 index ed9d89a..0000000 --- a/rvpn/connection/setup.go +++ /dev/null @@ -1,17 +0,0 @@ -package connection - -import ( - "log" - "os" -) - -var ( - loginfo *log.Logger - logdebug *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile -) - -func init() { - loginfo = log.New(os.Stdout, "INFO: connection: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: connection:", logFlags) -} diff --git a/rvpn/connectiontrack/setup.go b/rvpn/connectiontrack/setup.go deleted file mode 100644 index 54f10fe..0000000 --- a/rvpn/connectiontrack/setup.go +++ /dev/null @@ -1,17 +0,0 @@ -package connectiontrack - -import ( - "log" - "os" -) - -var ( - loginfo *log.Logger - logdebug *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile -) - -func init() { - loginfo = log.New(os.Stdout, "INFO: connectiontrack: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: connectiontrack:", logFlags) -} diff --git a/rvpn/external/listener_webrequest.go b/rvpn/external/listener_webrequest.go deleted file mode 100644 index 9175c05..0000000 --- a/rvpn/external/listener_webrequest.go +++ /dev/null @@ -1,82 +0,0 @@ -package external - -import ( - "fmt" - "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, connectionTable *connection.Table) { - loginfo.Println("starting WebRequestExternal Listener ", *serverBinding) - - 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) - } - - 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("Domain Accepted") - loginfo.Println(conn, rAddr, rPort) - p := packer.NewPacker() - p.Header.SetAddress(rAddr) - p.Header.Port, err = strconv.Atoi(rPort) - p.Header.Port = 8080 - p.Header.Service = "http" - p.Data.AppendBytes(dump) - buf := p.PackV1() - - sendTrack := connection.NewSendTrack(buf.Bytes(), hostname) - conn.SendCh() <- sendTrack - } - } - }) - s := &http.Server{ - Addr: *serverBinding, - Handler: mux, - ConnState: connState, - } - - err := s.ListenAndServe() - if err != nil { - loginfo.Println("ListenAndServe: ", err) - panic(err) - } -} - -func connState(conn net.Conn, state http.ConnState) { - loginfo.Println("connState") - fmt.Println(conn, conn.LocalAddr(), conn.RemoteAddr()) - fmt.Println(state) -} diff --git a/rvpn/external/server.go b/rvpn/external/server.go deleted file mode 100644 index 160c5b3..0000000 --- a/rvpn/external/server.go +++ /dev/null @@ -1,174 +0,0 @@ -package external - -import ( - "net" - "net/http" - "strconv" - "strings" - - "bytes" - - "bufio" - - "git.daplie.com/Daplie/go-rvpn-server/rvpn/connection" - "git.daplie.com/Daplie/go-rvpn-server/rvpn/packer" -) - -//LaunchExternalServer -- used to listen for external connections destin for WSS -func LaunchExternalServer(serverBinding string, connectionTable *connection.Table) { - addr, err := net.ResolveTCPAddr("tcp4", serverBinding) - if err != nil { - loginfo.Println("Unabled to resolve ", serverBinding, " in launchExternalServer") - loginfo.Println(err) - return - } - - loginfo.Println("passed ResolveTCPAddr") - - listener, err := net.ListenTCP("tcp", addr) - if err != nil { - loginfo.Println("unable to bind ", serverBinding) - return - } - - loginfo.Println("listening") - - for { - conn, err := listener.AcceptTCP() - if err != nil { - loginfo.Println("Bad accept ", err) - continue - } - - go handleConnection(conn, connectionTable) - } -} - -type protocol int - -//Family -- ENUM for Address Family -const ( - protoHTTP protocol = iota + 1 - protoHTTPS - protoSSLV3 - protoTLSV1 - protoTLSV11 - protoTLSV2 -) - -//State -- state of connection -type State struct { - Protocol protocol -} - -//NewState -- Constructor -func NewState() (p *State) { - p = new(State) - return -} - -func handleConnection(conn net.Conn, connectionTable *connection.Table) { - defer conn.Close() - - state := NewState() - - var buffer [512]byte - - for { - cnt, err := conn.Read(buffer[0:]) - if err != nil { - return - } - loginfo.Println("state ", state, " ", state.Protocol) - loginfo.Println("conn ", conn) - loginfo.Println("byte read", cnt) - //loginfo.Println("buffer") - //loginfo.Println(hex.Dump(buffer[0:cnt])) - - if state.Protocol == 0 { - //attempt to discover protocol - - // HTTP Identifcation - if bytes.Contains(buffer[:], []byte{0x0d, 0x0a}) { - //string protocol - if bytes.ContainsAny(buffer[:], "HTTP/") { - loginfo.Println("identifed HTTP") - state.Protocol = protoHTTP - } - - } else if bytes.Contains(buffer[:], []byte{0x16, 0x03, 0x00}) { - loginfo.Println("identifed SSLV3") - state.Protocol = protoSSLV3 - - } else if bytes.Contains(buffer[:], []byte{0x16, 0x03, 0x01}) { - loginfo.Println("identifed TLSV1") - state.Protocol = protoTLSV1 - - } else if bytes.Contains(buffer[:], []byte{0x16, 0x03, 0x02}) { - loginfo.Println("identifed TLSV1.1") - state.Protocol = protoTLSV11 - - } else if bytes.Contains(buffer[:], []byte{0x16, 0x03, 0x03}) { - loginfo.Println("identifed TLSV2") - state.Protocol = protoTLSV2 - - } else { - loginfo.Println("Protocol not identified", conn) - return - } - } - - if state.Protocol == 0 { - loginfo.Println("Making sure protocol is set") - loginfo.Println(state) - return - } else if state.Protocol == protoHTTP { - readBuffer := bytes.NewBuffer(buffer[0:cnt]) - reader := bufio.NewReader(readBuffer) - r, err := http.ReadRequest(reader) - - loginfo.Println(r) - - if err != nil { - loginfo.Println("error parsing request") - return - } - - hostname := r.Host - loginfo.Println("Host: ", hostname) - - if strings.Contains(hostname, ":") { - arr := strings.Split(hostname, ":") - hostname = arr[0] - } - - loginfo.Println("Remote: ", conn.RemoteAddr().String()) - - remoteSplit := strings.Split(conn.RemoteAddr().String(), ":") - 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(, "Domain not supported", http.StatusBadRequest) - - } else { - - loginfo.Println("Domain Accepted") - loginfo.Println(conn, rAddr, rPort) - p := packer.NewPacker() - p.Header.SetAddress(rAddr) - p.Header.Port, err = strconv.Atoi(rPort) - p.Header.Port = 8080 - p.Header.Service = "http" - p.Data.AppendBytes(buffer[0:cnt]) - buf := p.PackV1() - - sendTrack := connection.NewSendTrack(buf.Bytes(), hostname) - conn.SendCh() <- sendTrack - } - } - - } -} diff --git a/rvpn/external/setup.go b/rvpn/external/setup.go deleted file mode 100644 index 1bff29b..0000000 --- a/rvpn/external/setup.go +++ /dev/null @@ -1,17 +0,0 @@ -package external - -import ( - "log" - "os" -) - -var ( - loginfo *log.Logger - logdebug *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile -) - -func init() { - loginfo = log.New(os.Stdout, "INFO: external: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: external:", logFlags) -} \ No newline at end of file diff --git a/rvpn/xlate/wss_mapping.go b/rvpn/genericlistener/wss_mapping.go similarity index 94% rename from rvpn/xlate/wss_mapping.go rename to rvpn/genericlistener/wss_mapping.go index 60b7d27..0c96026 100644 --- a/rvpn/xlate/wss_mapping.go +++ b/rvpn/genericlistener/wss_mapping.go @@ -1,12 +1,7 @@ -package xlate +package genericlistener import "golang.org/x/net/websocket" -const ( - initialDomains = 10 - incrementDomains = 10 -) - type domain string //WssRegistration -- diff --git a/rvpn/instrumentation/setup.go b/rvpn/instrumentation/setup.go deleted file mode 100755 index bec78c7..0000000 --- a/rvpn/instrumentation/setup.go +++ /dev/null @@ -1,17 +0,0 @@ -package instrumentation - -import ( - "log" - "os" -) - -var ( - loginfo *log.Logger - logdebug *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile -) - -func init() { - loginfo = log.New(os.Stdout, "INFO: instrumentation: ", logFlags) - logdebug = log.New(os.Stdout, "DEBUG: instrumentation:", logFlags) -} \ No newline at end of file diff --git a/rvpn/matching/protocol.go b/rvpn/matching/protocol.go deleted file mode 100644 index d337e1f..0000000 --- a/rvpn/matching/protocol.go +++ /dev/null @@ -1,62 +0,0 @@ -package matching - -type headerTerm int -type matchType int -type protocolType int - -//headerTerm -- ENUM for header terminator -const ( - CRLF2 headerTerm = iota - ZER0 -) - -//Family -- ENUM for Address Family -const ( - BYTES matchType = iota - REGEX -) - -const ( - pHTTP = iota + 1 - pTLS - pSSH -) - -//Protocol -- -type Protocol struct { - HeaderTerm headerTerm - MatchType matchType - Type protocolType - SearchRegex string - SearchBytes []byte -} - -//NewProtocol -- Constructor -func NewProtocol() (p *Protocol) { - p = new(Protocol) - return -} - -//Protocols -- -type Protocols struct { - protocols []*Protocol -} - -func (p *Protocols) add(protocol *Protocol) []*Protocol { - p.protocols = append(p.protocols, protocol) - return p.protocols -} - -//NewProtocols -- -func NewProtocols() (p *Protocols) { - p = new(Protocols) - p.protocols = make([]*Protocol, 0) - - newp := NewProtocol() - newp.MatchType = REGEX - newp.HeaderTerm = CRLF2 - newp.MatchType = pHTTP - p.add(newp) - - return -} diff --git a/rvpn/matching/setup.go b/rvpn/matching/setup.go deleted file mode 100644 index 4447d71..0000000 --- a/rvpn/matching/setup.go +++ /dev/null @@ -1,13 +0,0 @@ -package matching - -import "log" -import "os" - -func init() { - logFlags := log.Ldate | log.Lmicroseconds | log.Lshortfile - loginfo := log.New(os.Stdout, "INFO: matching: ", logFlags) - logdebug := log.New(os.Stdout, "DEBUG: matching:", logFlags) - - loginfo.Println("") - logdebug.Println("") -}