This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
177 lines (153 loc) · 4.47 KB
/
api.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main;
import (
"net/http"
"github.com/bugsnag/bugsnag-go"
"github.com/gorilla/websocket"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"os"
"log"
"encoding/json"
"github.com/rs/cors"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func wsProxy(from*websocket.Conn, to *websocket.Conn) {
defer from.Close();
defer to.Close();
for {
messageType, bytes, err := from.ReadMessage();
if err != nil {
break
}
to.WriteMessage(messageType, bytes)
}
}
func remoteSshLifelineHandler(name string, w http.ResponseWriter, conn *websocket.Conn) {
var lf, err = registryGet(name)
if (err != nil){
conn.WriteMessage(websocket.TextMessage, []byte("lifeline " + name + " not registered."))
conn.Close()
return;
}
var lifelined = lf["lifelined"].(string);
if (lifelined == Config.WanAddr) {
conn.WriteMessage(websocket.TextMessage, []byte(name + " is registered at myself, but i don't have it. please try again later."))
conn.Close()
return;
}
conn.WriteMessage(websocket.TextMessage, []byte(name + " is registered at " + lf["lifelined"].(string) + ". rerouting you there now."))
c, _, err := websocket.DefaultDialer.Dial("ws://" + lf["lifelined"].(string) + "/lifelines/" + name + "/ssh", nil)
if err != nil {
log.Println("dial:", err)
conn.WriteMessage(websocket.TextMessage, []byte("other lifelined did not respond. try again later"))
return;
}
go wsProxy(conn, c);
go wsProxy(c, conn);
}
func wsWriter(conn *websocket.Conn, lf *Lifeline) {
defer conn.Close();
lf.Enable();
for {
b := <-lf.cx
if (len(b) < 1) {
break;
}
conn.WriteMessage(websocket.BinaryMessage, b)
}
}
func wsReader(conn *websocket.Conn, lf *Lifeline) {
defer conn.Close();
defer lf.Release(true);
for {
messageType, bytes, err := conn.ReadMessage();
if err != nil {
break
}
if messageType == websocket.BinaryMessage {
lf.conn.Write(bytes)
}
}
}
func sshLifelineHandler(w http.ResponseWriter, r *http.Request) {
var vars = mux.Vars(r)
var name = vars["name"]
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
conn.WriteMessage(websocket.TextMessage, []byte("helo. this is " + Config.WanAddr))
lifelinesMutex.Lock()
var lf *Lifeline = lifelines[name]
lifelinesMutex.Unlock()
if (lf == nil) {
remoteSshLifelineHandler(name, w, conn);
return
}
conn.WriteMessage(websocket.TextMessage, []byte("your lifeline is local. dispatching now."))
go wsReader(conn, lf);
go wsWriter(conn, lf);
}
func getLifelineHandler(w http.ResponseWriter, r *http.Request) {
var vars = mux.Vars(r)
var name = vars["name"]
var lf, err = registryGet(name)
if err != nil {
log.Println(err)
w.WriteHeader(404);
return;
}
_, local := lifelines[name]
json.NewEncoder(w).Encode(struct{
Self string `json:"self"`
Lifeline map[string]interface{} `json:"lifeline"`
Local bool `json:"local"`
}{
Config.WanAddr,
lf,
local,
})
if err != nil {
log.Println(err)
w.WriteHeader(500);
return;
}
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200);
keys := make([]string, len(lifelines))
i := 0
for k := range lifelines {
keys[i] = k
i++
}
json.NewEncoder(w).Encode(struct{
Self string `json:"self"`
Locals []string `json:"locals"`
}{
Config.WanAddr,
keys,
})
}
func ApiServer() {
corses := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "OPTIONS"},
AllowedHeaders: []string{"accept", "authorization", "Content-Type"},
Debug: true,
})
r := mux.NewRouter()
r.HandleFunc("/", statusHandler)
r.HandleFunc("/lifelines/{name}", getLifelineHandler)
r.HandleFunc("/lifelines/{name}/ssh", sshLifelineHandler)
log.Println("apiserver on", Config.ApiAddr());
log.Fatal(http.ListenAndServe(Config.ApiAddr(), bugsnag.Handler(
handlers.LoggingHandler(os.Stderr,
corses.Handler(
r)))));
}