forked from pterodactyl/sftp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (75 loc) · 1.88 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
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"path"
"strings"
)
type config struct {
ReadOnly bool `json:"readOnly"`
Port int `json:"port"`
BindAddress string `json:"bind"`
Filepath string `json:"files"`
User struct {
UID int `json:"uid"`
GID int `json:"gid"`
} `json:"osUser"`
Users map[string]struct {
Password string `json:"password"`
Permissions []string `json:"permissions"`
} `json:"users"`
}
func main() {
// Read the config.json.
var conf config
configFile, err := ioutil.ReadFile("config.json")
if err != nil {
panic(err)
}
json.Unmarshal(configFile, &conf)
server := &Server{
Settings: Settings{
ReadOnly: conf.ReadOnly,
BindAddress: conf.BindAddress,
BindPort: conf.Port,
BasePath: conf.Filepath,
},
User: User{ // The SFTP server runs `chown` to this user on any uploaded file.
UID: conf.User.UID,
GID: conf.User.GID,
},
PathValidator: func(fs *FileSystem, p string) (string, error) {
join := path.Join(conf.Filepath, p)
clean := path.Clean(conf.Filepath)
if strings.HasPrefix(join, clean) {
return join, nil
}
return "", errors.New("invalid path outside the configured directory was provided")
},
DiskSpaceValidator: func(fs *FileSystem) bool {
return true // TODO
},
CredentialValidator: func(r AuthenticationRequest) (*AuthenticationResponse, error) {
user, exists := conf.Users[r.User]
if !exists || fmt.Sprintf("%x", sha256.Sum256([]byte(r.Pass))) != user.Password {
return nil, InvalidCredentialsError{}
}
n, _ := rand.Int(rand.Reader, big.NewInt(9223372036854775807))
return &AuthenticationResponse{
Server: "none",
Token: n.String(),
Permissions: user.Permissions,
}, nil
},
}
err = New(server)
if err != nil {
panic(err)
}
panic(server.Initialize())
}