forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (98 loc) · 3.44 KB
/
main.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
/*
* Copyright (C) 2017 Jianhui Zhao <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package main
import (
"flag"
"log"
"time"
"strconv"
"math/rand"
"net/http"
"crypto/md5"
"encoding/hex"
"encoding/json"
_ "github.com/zhaojh329/rttys/statik"
"github.com/rakyll/statik/fs"
)
type DeviceInfo struct {
ID string `json:"id"`
Uptime int64 `json:"uptime"`
Description string `json:"description"`
}
func generateSID(devid string) string {
md5Ctx := md5.New()
md5Ctx.Write([]byte(devid + strconv.FormatFloat(rand.Float64(), 'e', 6, 32)))
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}
func main() {
port := flag.Int("port", 5912, "http service port")
cert := flag.String("cert", "", "certFile Path")
key := flag.String("key", "", "keyFile Path")
flag.Parse()
rand.Seed(time.Now().Unix())
log.Println("rttys version:", rttys_version())
br := newBridge()
go br.run()
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
return
}
staticfs := http.FileServer(statikFS)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
if t := r.URL.Query().Get("t"); t == "" {
http.Redirect(w, r, "/?t=" + strconv.FormatInt(time.Now().Unix(), 10), http.StatusFound)
return
}
}
staticfs.ServeHTTP(w, r)
})
http.HandleFunc("/devs", func(w http.ResponseWriter, r *http.Request) {
devs := make([]DeviceInfo, 0)
for _, c := range br.devices {
if c.isDev {
d := DeviceInfo{c.devid, time.Now().Unix() - c.timestamp, c.description}
devs = append(devs, d)
}
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("content-type", "application/json")
js, _ := json.Marshal(devs)
w.Write(js)
})
http.HandleFunc("/cmd", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("content-type", "application/json")
serveCmd(br, w, r)
})
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(br, w, r)
})
if *cert != "" && *key != "" {
log.Println("Listen on: ", *port, "SSL on")
log.Fatal(http.ListenAndServeTLS(":" + strconv.Itoa(*port), *cert, *key, nil))
} else {
log.Println("Listen on: ", *port, "SSL off")
log.Fatal(http.ListenAndServe(":" + strconv.Itoa(*port), nil))
}
}