-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (118 loc) · 3.67 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
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/rs/cors"
"io/ioutil"
"net/http"
"os"
"sssh_server/CustomUtils"
"sssh_server/Modules/Configuration"
"sssh_server/Modules/Logging"
"sssh_server/Programs"
"sssh_server/SessionModules/RPC"
"sssh_server/SessionModules/SessionLayer"
)
var sessionService *SessionLayer.SessionService
var rpc *RPC.RPC
var config Configuration.Configuration
func init() {
//recentCommandsSrvc.Socket = Sockets.NewSocketReadWriter()
}
// This function adds a new command to the recent commands
// This is called from http from the localhost
// The bash command detects a new command and reports it back to the server
func newCommand(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
CustomUtils.CheckPrint(err)
_, _ = fmt.Fprintln(w, "ok")
flusher, _ := w.(http.Flusher)
flusher.Flush()
id := r.URL.Query().Get("SSSH_USER")
sessionService.AddCommand(string(b), id)
//rpc.OnCommand(string(b))
}
func variables(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
CustomUtils.CheckPrint(err)
_, _ = fmt.Fprintln(w, "ok")
flusher, _ := w.(http.Flusher)
flusher.Flush()
id := r.URL.Query().Get("SSSH_USER")
sessionService.UpdateVariables(string(b), id)
//rpc.OnCommand(string(b))
}
func pwd(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
CustomUtils.CheckPrint(err)
id := r.URL.Query().Get("SSSH_USER")
sessionService.UpdatePWD(string(b), id)
//rpc.OnCommand(string(b))
}
func getPublickKey(w http.ResponseWriter, r *http.Request) {
pubKey, e := sessionService.GetPubKey()
CustomUtils.CheckPanic(e, "Couldn't read pub key:")
pubKeyJson, e := json.Marshal(pubKey)
CustomUtils.CheckPanic(e, "Couldn't marshal pub key:")
_, _ = w.Write(pubKeyJson)
}
// GetIP gets a requests IP address by reading off the forwarded-for
// header (for proxies) and falls back to use the remote address.
func getIP(r *http.Request) string {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}
func stop(w http.ResponseWriter, r *http.Request) {
ip := getIP(r)
if !(ip == "localhost" || ip == "127.0.0.1") {
_ = httpServer.Shutdown(context.Background())
}
sessionService.Close()
os.Exit(0)
}
var httpServer http.Server
func server(config Configuration.Configuration) {
//r := mux.NewRouter()
CustomUtils.Logger.Printlnf(Logging.INFO, "Serving at localhost:%v", config.Port)
mux := http.NewServeMux()
sessionService = SessionLayer.Constructor(config.KeyFile, config.Port)
// needed http
mux.HandleFunc("/newcommand", newCommand)
mux.HandleFunc("/variables", variables)
mux.HandleFunc("/pwd", pwd)
mux.HandleFunc("/stop", stop)
mux.HandleFunc("/pubKey", getPublickKey)
handler := cors.Default().Handler(mux)
go sessionService.Serve()
httpServer := http.Server{Addr: fmt.Sprintf(":%v", config.HTTPPort), Handler: handler}
e := httpServer.ListenAndServe()
CustomUtils.CheckPanic(e, "Server stop")
CustomUtils.Logger.Printlnf(Logging.INFO, "Stop")
}
func main() {
config = Configuration.Configuration{}
config.Init()
rpc = RPC.New(config.RPCPort)
for _, service := range SessionLayer.CommandServices {
rpc.AddService(service)
}
if config.Mode == "server" {
CustomUtils.Logger.Println(Logging.INFO, fmt.Sprint(os.Args))
CustomUtils.Logger.Println(Logging.INFO, config.String())
server(config)
} else if config.Mode == "prompt" {
Programs.Prompt(config)
} else if config.Mode == "keygen" {
Programs.Keygen(config)
} else if config.Mode == "fingerprint" {
Programs.Fingerprint(config)
} else if config.Mode == "stop" {
Programs.Stop(config)
} else {
panic("Invalid Mode " + config.Mode)
}
}