-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.go
160 lines (126 loc) · 3.29 KB
/
http.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
package main
import (
"crypto/tls"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"log"
"net"
"net/http"
"errors"
"os"
"html/template"
)
var (
tlsCertPath string
tlsKeyPath string
webRoot = "web"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func handleWebsocketConnection(conn *websocket.Conn) {
client := NewWebsocketClientConnection(*conn)
client.read()
}
func httpServeHomeFunc(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, webRoot+"/dist/index.html")
}
func httpServeCharVerification(w http.ResponseWriter, r *http.Request) {
type Page struct {
Char Sc2Char
Error string
}
var (
p Page
err error
char Sc2Char
)
state := r.URL.Query()["state"][0]
// Get the OAuth for the state requested
if oar, ok := activeOAuths[state]; ok {
var proto_char *BattleNetCharacter
char, proto_char, err = oar.getCharInfo(r.URL.Query()["code"][0])
if err != nil {
oar.conn.logger.Println(err)
} else if proto_char != nil {
payload := proto_char.CharacterMessage()
data, _ := Marshal(payload)
oar.conn.SendResponseMessage("BNN", -1, data)
}
} else {
err = errors.New("This is not a valid request, please try again.")
}
t, templ_err := template.ParseFiles(webRoot + "dist/verify_char.html")
if templ_err != nil {
log.Println(templ_err)
}
if err != nil {
p.Error = err.Error()
}
p.Char = char
t.Execute(w, p)
}
func httpServeWsFunc(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Failed to upgrade WS from", r.RemoteAddr, err)
return
}
log.Println("Accepted WS from", ws.RemoteAddr())
go handleWebsocketConnection(ws)
}
func setupRouter(r *mux.Router) {
r.HandleFunc("/ws", httpServeWsFunc).Methods("GET")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(webRoot+"/dist")))).Methods("GET")
r.HandleFunc("/login/battlenet", httpServeCharVerification).Methods("GET")
r.HandleFunc("/{path:.*}", httpServeHomeFunc).Methods("GET")
}
func listenAndServeHTTP(address string) {
if testMode {
upgrader.CheckOrigin = func(r *http.Request) bool {
// allow all connections in testmode
return true
}
}
r := mux.NewRouter()
setupRouter(r)
log.Println("Listening HTTP on", address)
go http.ListenAndServe(address, r)
// log.Fatalln()
}
func listenAndServeHTTPS(address string) error {
if _, err := os.Stat(tlsKeyPath); err == nil {
r := mux.NewRouter()
setupRouter(r)
srv := &http.Server{Addr: address, Handler: r}
addr := srv.Addr
config := &tls.Config{}
if srv.TLSConfig != nil {
*config = *srv.TLSConfig
}
if config.NextProtos == nil {
config.NextProtos = []string{"http/1.1"}
}
var err error
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(tlsCertPath, tlsKeyPath)
if err != nil {
log.Fatalln(err)
}
ln, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalln(err)
}
tlsListener := tls.NewListener(ln, config)
go srv.Serve(tlsListener)
log.Println("Listening HTTPS on", address)
} else {
if testMode {
log.Println("Could not use https certs: ", err, " (proceeding anyway due to test mode)")
} else {
log.Fatalln("Could not use https certs: ", err)
}
}
return nil
}