-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
59 lines (50 loc) · 1.49 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
flag "github.com/spf13/pflag"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
runAsServer := flag.BoolP(`server`, `s`, false, `Run as server. [S]`)
runAsClient := flag.BoolP(`client`, `c`, false, `Run as client. [C]`)
serverEndpoint := flag.StringP(`endpoint`, `e`, ``, `Server endpoint. [C]`)
token := flag.StringP(`token`, `t`, ``, `The token used between client and server [SC]`)
userAgent := flag.String(`user-agent`, ``, `Use this User-Agent instead of the default Go-http-client/1.1 [C]`)
listenAddr := flag.StringP(`listen`, `l`, ``, `Listen address [SC]`)
destination := flag.StringP(`destination`, `d`, ``, `The destination address to connect to [C]`)
help := flag.BoolP(`help`, `h`, false, `Show this help`)
flag.CommandLine.SortFlags = false
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "[S]: server side flag.\n[C]: client side flag.")
os.Exit(1)
}
flag.Parse()
if !*runAsServer && !*runAsClient || *help {
flag.Usage()
return
}
if *runAsServer {
s := NewServer(*token)
if err := http.ListenAndServe(*listenAddr, s); err != nil {
log.Fatalln(err)
}
return
}
if *runAsClient {
c := NewClient(*serverEndpoint, *token)
c.SetUserAgent(*userAgent)
if *listenAddr != `` {
c.Serve(*listenAddr, *destination)
} else {
c.Std(*destination)
}
return
}
}