-
Notifications
You must be signed in to change notification settings - Fork 2
/
mux.go
171 lines (157 loc) · 4.1 KB
/
mux.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
package samtunnelhandler
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
)
type TunnelHandlerMux struct {
http.Server
pagenames []string
tunnels []*TunnelHandler
user string
password string
sessionToken string
//cssString string
//jsString string
templateTop string
templateBot string
templateTopHTML *template.Template
templateBotHTML *template.Template
}
func (m *TunnelHandlerMux) ListenAndServe() {
m.Server.ListenAndServe()
}
func (m *TunnelHandlerMux) Count() int {
return len(m.tunnels)
}
func (m *TunnelHandlerMux) User() string {
if m.user == "" {
return "anonymous"
}
return m.user
}
func (m *TunnelHandlerMux) CheckCookie(w http.ResponseWriter, r *http.Request) bool {
if m.password != "" {
if m.sessionToken == "" {
w.WriteHeader(http.StatusUnauthorized)
return false
}
c, err := r.Cookie("session_token")
if err != nil {
if err == http.ErrNoCookie {
w.WriteHeader(http.StatusUnauthorized)
return false
}
w.WriteHeader(http.StatusBadRequest)
return false
}
if m.sessionToken != c.Value {
w.WriteHeader(http.StatusUnauthorized)
return false
}
}
return true
}
func (m *TunnelHandlerMux) HandlerWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if m.CheckCookie(w, r) == false {
return
}
h.ServeHTTP(w, r)
})
}
func (t *TunnelHandlerMux) Tunnels() []*TunnelHandler {
return t.tunnels
}
func (m *TunnelHandlerMux) Append(v *TunnelHandler) *TunnelHandlerMux {
if m == nil {
return m
}
for _, prev := range m.tunnels {
if v.ID() == prev.ID() {
log.Printf("v.ID() found, %s == %s", v.ID(), prev.ID())
return m
}
}
log.Printf("Adding tunnel ID: %s", v.ID())
m.tunnels = append(m.tunnels, v)
Handler := m.Handler.(*http.ServeMux)
Handler.Handle(fmt.Sprintf("/%d", len(m.tunnels)), m.HandlerWrapper(v))
Handler.Handle(fmt.Sprintf("/%s", v.ID()), m.HandlerWrapper(v))
Handler.Handle(fmt.Sprintf("/%d/color", len(m.tunnels)), m.HandlerWrapper(v))
Handler.Handle(fmt.Sprintf("/%s/color", v.ID()), m.HandlerWrapper(v))
m.Handler = Handler
return m
}
func ReadFile(filename string) (string, error) {
r, e := ioutil.ReadFile(filename)
return string(r), e
}
func NewTunnelHandlerMux(host, port, user, password, css, javascript string) *TunnelHandlerMux {
var m TunnelHandlerMux
m.Addr = host + ":" + port
Handler := http.NewServeMux()
m.pagenames = []string{"index.html", "index", ""}
m.user = user
m.password = password
m.sessionToken = ""
m.tunnels = []*TunnelHandler{}
m.templateTop = `
<head>
<meta charset="utf-8">
<title>SAMTunnel</title>
<link rel="stylesheet" href="/styles.css">
<script src="/scripts.js"></script>
</head>
<body>
<h1>SAMTunnel</h1>
<a class="samtunnel-home-page" href="/index.html">Welcome {{.User}}! you are serving {{.Count}} tunnels. </a>
<div id="toggleall" class="global control">
<a class="samtunnel-global-toggle" href="#" onclick="toggle_visibility_class('prop');">Show/Hide All</a>
</div>
`
m.templateBot = `</body>
</html>
`
var err error
m.templateTopHTML = template.Must(template.New("top").Parse(m.templateTop))
if err != nil {
log.Printf("Error parsing templateTop: %s", err)
}
m.templateBotHTML = template.Must(template.New("bot").Parse(m.templateBot))
if err != nil {
log.Printf("Error parsing templateBot: %s", err)
}
for _, v := range m.pagenames {
Handler.HandleFunc(fmt.Sprintf("/%s", v), m.Home)
}
Handler.HandleFunc("/styles.css", m.CSS)
Handler.HandleFunc("/scripts.js", m.JS)
if m.password != "" {
Handler.HandleFunc("/login", m.Signin)
}
m.Handler = Handler
return &m
}
func (m *TunnelHandlerMux) Home(w http.ResponseWriter, r *http.Request) {
if m.CheckCookie(w, r) == false {
return
}
if r.URL.Path == "/" {
http.Redirect(w, r, "/index.html", 301)
fmt.Fprintf(w, "redirecting to index.html")
return
}
r2, err := http.NewRequest("GET", r.URL.Path+"/color", r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
m.templateTopHTML.Execute(w, m)
for _, tunnel := range m.Tunnels() {
tunnel.ServeHTTP(w, r2)
}
m.templateBotHTML.Execute(w, m)
}