Skip to content

Commit 7e8b832

Browse files
committed
feat: Supports TLS.
1 parent 45f8bbc commit 7e8b832

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Prerequisites: A server: Windows or Linux
1111
2、Run
1212

1313
```bash
14+
# http
1415
GUI-Sync --address 0.0.0.0 --port 8080 --token "A unique string"
16+
17+
# https
18+
GUI-Sync --address 0.0.0.0 --port 8080 --token "A unique string" --cert /path/to/serer.cert --key /path/to/server.key
1519
```
1620

1721
Note: The `token` needs to be consistent with the client.

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var (
1616
Addrss = "0.0.0.0"
1717
Port = 8080
1818
Token = ""
19+
Cert = ""
20+
Key = ""
1921
)
2022

2123
type BackupEntry struct {
@@ -27,6 +29,8 @@ type BackupEntry struct {
2729
func main() {
2830
flag.StringVar(&Token, "token", "", "Authorization")
2931
flag.StringVar(&Addrss, "address", "0.0.0.0", "Address to listen on")
32+
flag.StringVar(&Cert, "cert", "", "Cert file path")
33+
flag.StringVar(&Key, "key", "", "Key file path")
3034
flag.IntVar(&Port, "port", 8080, "Port to listen on")
3135
flag.Parse()
3236

@@ -39,7 +43,16 @@ func main() {
3943

4044
http.HandleFunc("/backup", withAuth(handleBackup))
4145
http.HandleFunc("/sync", withAuth(handleSync))
42-
http.ListenAndServe(fmt.Sprintf("%s:%d", Addrss, Port), nil)
46+
47+
var err error
48+
if Cert != "" && Key != "" {
49+
err = http.ListenAndServeTLS(fmt.Sprintf("%s:%d", Addrss, Port), Cert, Key, nil)
50+
} else {
51+
err = http.ListenAndServe(fmt.Sprintf("%s:%d", Addrss, Port), nil)
52+
}
53+
if err != nil {
54+
log.Print(err.Error())
55+
}
4356
}
4457

4558
func withAuth(next http.HandlerFunc) http.HandlerFunc {

0 commit comments

Comments
 (0)