WIP lots of debugging, fix case of previously existing server

This commit is contained in:
AJ ONeal 2020-07-12 05:59:34 +00:00
parent d3a218e73d
commit 3ac48f0db1
6 changed files with 53 additions and 5 deletions

View File

@ -168,7 +168,18 @@ func upgradeWebsocket(w http.ResponseWriter, r *http.Request) {
return return
} }
//fmt.Printf("LocalAddr: %#v\n", r.LocalAddr)
wsTun := telebit.NewWebsocketTunnel(conn) wsTun := telebit.NewWebsocketTunnel(conn)
fmt.Printf("[debug] http.req.RemoteAddr: %+v\n", r.RemoteAddr)
fmt.Printf("[debug] conn.RemoteAddr(): %+v\n", conn.RemoteAddr())
fmt.Printf("[debug] conn.LocalAddr(): %+v\n", conn.LocalAddr())
//fmt.Printf("wsTun.RemoteAddr(): %+v\n", wsTun.RemoteAddr())
//fmt.Printf("wsTun.LocalAddr(): %#v\n", wsTun.LocalAddr())
// The remote address of the server is useful for identification.
// The local address of the server (port to which it connected) is not very meaningful.
// Rather the client's local address (the specific relay server) would be more useful.
server := &table.SubscriberConn{ server := &table.SubscriberConn{
RemoteAddr: r.RemoteAddr, RemoteAddr: r.RemoteAddr,
WSConn: conn, WSConn: conn,
@ -192,6 +203,8 @@ func upgradeWebsocket(w http.ResponseWriter, r *http.Request) {
_ = wsTun.Close() _ = wsTun.Close()
// TODO close all clients // TODO close all clients
fmt.Printf("a subscriber stream is done: %q\n", err) fmt.Printf("a subscriber stream is done: %q\n", err)
// TODO check what happens when we leave a junk connection
table.Remove(server.Grants.Subject)
}() }()
table.Add(server) table.Add(server)

View File

@ -354,6 +354,9 @@ func routeSubscribersAndClients(client net.Conn) error {
//dstAddr := dstParts[0] //dstAddr := dstParts[0]
dstPort, _ := strconv.Atoi(dstParts[1]) dstPort, _ := strconv.Atoi(dstParts[1])
fmt.Printf("[debug] wconn.LocalAddr() %+v\n", wconn.LocalAddr())
fmt.Printf("[debug] wconn.RemoteAddr() %+v\n", wconn.RemoteAddr())
if 80 != dstPort && 443 != dstPort { if 80 != dstPort && 443 != dstPort {
// TODO handle by port without peeking at Servername / Hostname // TODO handle by port without peeking at Servername / Hostname
// if tryToServePort(client.LocalAddr().String(), wconn) { // if tryToServePort(client.LocalAddr().String(), wconn) {
@ -369,6 +372,7 @@ func routeSubscribersAndClients(client net.Conn) error {
return fmt.Errorf("invalid servername") return fmt.Errorf("invalid servername")
} }
fmt.Printf("[debug] wconn.Servername() %+v\n", servername)
// Match full servername "sub.domain.example.com" // Match full servername "sub.domain.example.com"
if tryToServeName(servername, wconn) { if tryToServeName(servername, wconn) {
// TODO better non-error // TODO better non-error
@ -407,6 +411,7 @@ func tryToServeName(servername string, wconn *telebit.ConnWrap) bool {
// async so that the call stack can complete and be released // async so that the call stack can complete and be released
//srv.clients.Store(wconn.LocalAddr().String(), wconn) //srv.clients.Store(wconn.LocalAddr().String(), wconn)
go func() { go func() {
fmt.Printf("[debug] found server to handle client:\n%#v\n", srv)
err := srv.Serve(wconn) err := srv.Serve(wconn)
fmt.Printf("a browser client stream is done: %q\n", err) fmt.Printf("a browser client stream is done: %q\n", err)
//srv.clients.Delete(wconn.LocalAddr().String()) //srv.clients.Delete(wconn.LocalAddr().String())

View File

@ -123,9 +123,12 @@ func (m *RouteMux) HandleTLS(servername string, acme *ACME, handler Handler) err
} }
if !wconn.isEncrypted() { if !wconn.isEncrypted() {
fmt.Println("[debug] conn is not encrypted")
return ErrNotHandled return ErrNotHandled
} }
fmt.Println("[debug] terminated encrypted connection")
//NewTerminator(acme, handler)(client) //NewTerminator(acme, handler)(client)
//return handler.Serve(client) //return handler.Serve(client)
return handler.Serve(TerminateTLS(wconn, acme)) return handler.Serve(TerminateTLS(wconn, acme))

View File

@ -340,6 +340,10 @@ func Inspect(authURL, token string) (*Grants, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if "" == grants.Subject {
fmt.Println("TODO update mgmt server to show Subject:", msg)
grants.Subject = strings.Split(grants.Domains[0], ".")[0]
}
return grants, nil return grants, nil
} }

View File

@ -117,14 +117,14 @@ func (wsw *WebsocketTunnel) Close() error {
func (wsw *WebsocketTunnel) LocalAddr() net.Addr { func (wsw *WebsocketTunnel) LocalAddr() net.Addr {
// TODO do we reverse this since the "local" address is that of the relay? // TODO do we reverse this since the "local" address is that of the relay?
// return wsw.wsconn.RemoteAddr() // return wsw.wsconn.RemoteAddr()
panic("LocalAddr() not implemented") panic("no LocalAddr() implementation")
} }
// RemoteAddr is not implemented and will panic. Additionally, it wouldn't mean anything useful anyway. // RemoteAddr is not implemented and will panic. Additionally, it wouldn't mean anything useful anyway.
func (wsw *WebsocketTunnel) RemoteAddr() net.Addr { func (wsw *WebsocketTunnel) RemoteAddr() net.Addr {
// TODO do we reverse this since the "remote" address means nothing / is that of one of the clients? // TODO do we reverse this since the "remote" address means nothing / is that of one of the clients?
// return wsw.wsconn.LocalAddr() // return wsw.wsconn.LocalAddr()
panic("RemoteAddr() not implemented") panic("no RemoteAddr() implementation")
} }
// SetDeadline sets the read and write deadlines associated // SetDeadline sets the read and write deadlines associated

View File

@ -36,21 +36,27 @@ func Add(server *SubscriberConn) {
Servers.Store(server.Grants.Subject, srvMap) Servers.Store(server.Grants.Subject, srvMap)
// Add this server to the domain name matrix // Add this server to the domain name matrix
for _, name := range server.Grants.Domains { for _, domainname := range server.Grants.Domains {
var srvMap *sync.Map var srvMap *sync.Map
srvMapX, ok := Table.Load(name) srvMapX, ok := Table.Load(domainname)
if ok { if ok {
srvMap = srvMapX.(*sync.Map) srvMap = srvMapX.(*sync.Map)
} else { } else {
srvMap = &sync.Map{} srvMap = &sync.Map{}
} }
srvMap.Store(server.RemoteAddr, server) srvMap.Store(server.RemoteAddr, server)
Table.Store(name, srvMap) Table.Store(domainname, srvMap)
} }
} }
func RemoveByAddr(subject string) bool {
// TODO
return false
}
func Remove(subject string) bool { func Remove(subject string) bool {
srvMapX, ok := Servers.Load(subject) srvMapX, ok := Servers.Load(subject)
fmt.Printf("[debug] has server for %s? %t\n", subject, ok)
if !ok { if !ok {
return false return false
} }
@ -64,6 +70,23 @@ func Remove(subject string) bool {
return true return true
}) })
srv.WSConn.Close() srv.WSConn.Close()
for _, domainname := range srv.Grants.Domains {
srvMapX, ok := Table.Load(domainname)
if !ok {
continue
}
srvMap = srvMapX.(*sync.Map)
srvMap.Delete(srv.RemoteAddr)
n := 0
srvMap.Range(func(k, v interface{}) bool {
n++
return true
})
if 0 == n {
// TODO comment out to handle the bad case of 0 servers / empty map
Table.Delete(domainname)
}
}
return true return true
}) })
Servers.Delete(subject) Servers.Delete(subject)