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
/
main.go
90 lines (77 loc) · 2.08 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
package main
import (
"flag"
"net"
"net/http"
"os"
"runtime"
"runtime/pprof"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/bit4bit/remoton"
"github.com/throttled/throttled"
"github.com/throttled/throttled/store"
)
var (
listenAddr = flag.String("listen", "localhost:9934", "listen address")
authTokenFlag = flag.String("auth-token", "", "authenticate API")
certFile = flag.String("cert", "cert.pem", "cert pem")
keyFile = flag.String("key", "key.pem", "key pem")
profile = flag.String("cpuprofile", "", "output profile to file")
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
if *profile != "" {
flag, err := os.Create(*profile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(flag)
defer pprof.StopCPUProfile()
}
if os.Getenv("REMOTON_SERVER_AUTH_TOKEN") != "" {
*authTokenFlag = os.Getenv("REMOTON_SERVER_AUTH_TOKEN")
}
if *authTokenFlag == "" {
*authTokenFlag = "public"
log.Println("Using default Token", *authTokenFlag)
}
if *certFile == "" || *keyFile == "" {
log.Error("need cert file and key file .pem")
return
}
th := throttled.RateLimit(throttled.PerMin(30),
&throttled.VaryBy{RemoteAddr: true},
store.NewMemStore(100),
)
mux := http.NewServeMux()
mux.Handle("/remoton/", http.StripPrefix("/remoton",
remoton.NewServer(func(authToken string, r *http.Request) bool {
return authToken == *authTokenFlag
}, func() string {
return remoton.GenerateAuthUser()
})))
log.Println("Listen at HTTPS ", *listenAddr)
sSecure := &http.Server{
Addr: *listenAddr,
Handler: th.Throttle(mux),
}
host, port, err := net.SplitHostPort(*listenAddr)
if err != nil {
log.Fatal(err)
}
iport, err := strconv.Atoi(port)
if err != nil {
panic(err)
}
listenInsecureAddr := net.JoinHostPort(host, strconv.Itoa(iport-1))
//Default insecure it's a previous port
log.Println("Listen at HTTP ", listenInsecureAddr)
sInsecure := &http.Server{
Addr: listenInsecureAddr,
Handler: th.Throttle(mux),
}
go sInsecure.ListenAndServe()
log.Fatal(sSecure.ListenAndServeTLS(*certFile, *keyFile))
}