This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
157 lines (131 loc) · 4.02 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package remoton
import (
"encoding/json"
"net"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
)
const (
timeoutDefaultListen = time.Minute * 20
timeoutDefaultDial = time.Minute * 3
)
type requestTunnel struct {
SessionID string
Service string
}
var (
chListenTunnel = make(chan requestTunnel)
chAcceptTunnel = make(chan net.Conn)
chDialTunnel = make(chan requestTunnel)
chDialAcceptTunnel = make(chan net.Conn)
)
var tunnelTypes map[string]func(net.Conn) http.Handler
// RegisterTunnelType for handling type of connections
func RegisterTunnelType(typ string, f func(net.Conn) http.Handler) {
if tunnelTypes == nil {
tunnelTypes = make(map[string]func(net.Conn) http.Handler)
}
tunnelTypes[typ] = f
}
//Server export API for handle connections
//this follow http.Handler can be embedded in any
//web app
type Server struct {
*httprouter.Router
//sessions handle sessions any session can have only 2 connections
//one for the producer and one for the consumer
sessions *sessionManager
idGenerator func() string
}
//NewServer create a new http.Listener, *authFunc* for custom authentication and
//idGenerator for identify connections
func NewServer(authFunc func(authToken string, r *http.Request) bool, idGenerator func() string) *Server {
r := &Server{httprouter.New(), NewSessionManager(), idGenerator}
r.RedirectFixedPath = false
r.POST("/session", hAuth(authFunc, r.hNewSession))
r.DELETE("/session/:id", hAuth(authFunc, r.hDestroySession))
r.GET("/session/:id/conn/:service/dial/:tunnel", r.hSessionDial)
r.GET("/session/:id/conn/:service/listen/:tunnel", r.hSessionListen)
return r
}
//hNewSession create a session and return ID:USERNAME:PASSWORD
func (c *Server) hNewSession(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
id := c.idGenerator()
c.sessions.Add(id, newSession(id))
resp := struct {
ID string
}{
ID: id,
}
data, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//hDestroySession destroy a session
//need header *X-Auth-Username* and *X-Auth-Password*
func (c *Server) hDestroySession(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if session := c.sessions.Get("id"); session != nil {
c.sessions.Del(params.ByName("id"))
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func (c *Server) hSessionDial(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
session := c.sessions.Get(params.ByName("id"))
if session == nil {
w.WriteHeader(http.StatusNotFound)
return
}
kservice := params.ByName("service")
if trans, ok := tunnelTypes[params.ByName("tunnel")]; ok {
listen, tunnel := net.Pipe()
service := session.Service(kservice)
select {
case service <- listen:
defer tunnel.Close()
trans(tunnel).ServeHTTP(w, r)
return
case <-time.After(timeoutDefaultDial):
w.WriteHeader(http.StatusGatewayTimeout)
return
}
}
w.WriteHeader(http.StatusInternalServerError)
}
func (c *Server) hSessionListen(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
session := c.sessions.Get(params.ByName("id"))
if session == nil {
w.WriteHeader(http.StatusNotFound)
return
}
kservice := params.ByName("service")
if trans, ok := tunnelTypes[params.ByName("tunnel")]; ok {
chtunnel := session.Service(kservice)
select {
case tunnel := <-chtunnel:
defer tunnel.Close()
trans(tunnel).ServeHTTP(w, r)
return
case <-time.After(timeoutDefaultListen):
w.WriteHeader(http.StatusGatewayTimeout)
return
}
}
w.WriteHeader(http.StatusInternalServerError)
}
func hAuth(authTokenFunc func(authToken string, r *http.Request) bool, handler httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if !authTokenFunc(r.Header.Get("X-Auth-Token"), r) {
w.WriteHeader(http.StatusUnauthorized)
return
}
handler(w, r, params)
}
}