-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
140 lines (120 loc) · 4.73 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright 2020 - 2021, Jan Bormet, Anna-Felicitas Hausmann, Joachim Schmidt, Vincent Stollenwerk, Arne Turuc
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/timewarrior-synchronize/timew-sync-server/storage"
"github.com/timewarrior-synchronize/timew-sync-server/sync"
"log"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
)
var versionFlag bool
var configFilePath string
var portNumber int
var keyDirectoryPath string
var dbPath string
var noAuth bool
var sourcePath string
var userID int64
func main() {
startCmd := flag.NewFlagSet("start", flag.ExitOnError)
addUserCmd := flag.NewFlagSet("add-user", flag.ExitOnError)
addKeyCmd := flag.NewFlagSet("add-key", flag.ExitOnError)
startCmd.StringVar(&configFilePath, "config-file", "", "[RESERVED, not used] Path to the configuration file")
startCmd.IntVar(&portNumber, "port", 8080, "Port on which the server will listen for connections")
startCmd.StringVar(&keyDirectoryPath, "keys-location", "authorized_keys", "Path to the users' public keys")
startCmd.StringVar(&dbPath, "sqlite-db", "db.sqlite", "Path to the SQLite database")
startCmd.BoolVar(&noAuth, "no-auth", false, "Run server without client authentication")
addUserCmd.StringVar(&sourcePath, "path", "", "Supply the path to a PEM RSA key")
addUserCmd.StringVar(&keyDirectoryPath, "keys-location", "authorized_keys", "Path to the users' public keys")
addKeyCmd.StringVar(&sourcePath, "path", "", "Supply the path to a PEM RSA key")
addKeyCmd.Int64Var(&userID, "id", -1, "Supply user id")
addKeyCmd.StringVar(&keyDirectoryPath, "keys-location", "authorized_keys", "Path to the users' public keys")
flag.BoolVar(&versionFlag, "version", false, "Print version information")
if len(os.Args) < 2 {
_, _ = fmt.Fprintf(os.Stderr, "Use commands start, add-user or add-key\n")
os.Exit(1)
}
switch os.Args[1] {
case "start":
_ = startCmd.Parse(os.Args[2:])
sync.PublicKeyLocation = keyDirectoryPath
case "add-user":
addUserCase(addUserCmd)
case "add-key":
addKeyCase(addKeyCmd)
default:
flag.Parse()
if versionFlag {
_, _ = fmt.Fprintf(os.Stderr, "timewarrior sync server version %v\n", "1.2.0")
os.Exit(0)
} else {
log.Fatal("Use commands start, add-user or add-key")
}
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatalf("Error while opening SQLite database: %v", err)
}
defer db.Close()
sqlStorage := &storage.Sql{DB: db}
err = sqlStorage.Initialize()
if err != nil {
log.Fatalf("Error while initializing database: %v", err)
}
storage.GlobalStorage = sqlStorage
handler := func(w http.ResponseWriter, req *http.Request) {
sync.HandleSyncRequest(w, req, noAuth)
}
http.HandleFunc("/api/sync", handler)
log.Printf("Listening on Port %v", portNumber)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", portNumber), nil))
}
// Subcommand for adding a new user
func addUserCase(addUserCmd *flag.FlagSet) {
_ = addUserCmd.Parse(os.Args[2:])
sync.PublicKeyLocation = keyDirectoryPath
id := sync.GetFreeUserID()
if sourcePath == "" {
sync.AddKey(id, "")
} else {
key := sync.ReadKey(sourcePath)
sync.AddKey(id, key)
}
_, _ = fmt.Fprintf(os.Stderr, "Successfully added new user %v", id)
os.Exit(0)
}
// Subcommand for adding a new key
func addKeyCase(addKeyCmd *flag.FlagSet) {
_ = addKeyCmd.Parse(os.Args[2:])
sync.PublicKeyLocation = keyDirectoryPath
if sourcePath == "" {
log.Fatal("Provide a key file with --path [path-to-key-file]")
}
if userID < 0 {
log.Fatal("Provide a non-negative user id with --id [user id]")
}
used := sync.GetUsedUserIDs()
if !used[userID] {
log.Fatalf("User %v does not exist", userID)
}
key := sync.ReadKey(sourcePath)
sync.AddKey(userID, key)
_, _ = fmt.Fprintf(os.Stderr, "Successfully added new key to user %v", userID)
os.Exit(0)
}