-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
175 lines (162 loc) · 3.8 KB
/
server.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2019 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"strings"
"github.com/kataras/golog"
"github.com/labstack/echo/v4"
md "github.com/labstack/echo/v4/middleware"
)
const (
XForwardedFor = "X-Forwarded-For"
XRealIP = "X-Real-IP"
)
type Result struct {
Version string `json:"version,omitempty"`
Message string `json:"message,omitempty"`
DeviceOn bool `json:"deviceon,omitempty"`
DefColor int64 `json:"defcolor,omitempty"`
DefIcon string `json:"deficon,omitempty"`
Btns []Btn `json:"btns,omitempty"`
}
func Logger(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var (
code int
err error
msg string
valid bool
)
req := c.Request()
if req.URL.String() == `/` {
return next(c)
}
remoteAddr := req.RemoteAddr
if ip := req.Header.Get(XRealIP); len(ip) > 6 {
remoteAddr = ip
} else if ip = req.Header.Get(XForwardedFor); len(ip) > 6 {
remoteAddr = ip
}
if strings.Contains(remoteAddr, ":") {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
sign := strings.ToLower(c.QueryParam(`hash`))
forHash := cfg.Password
device := c.QueryParam(`device`)
key := c.QueryParam(`key`)
if len(cfg.Devices) > 0 {
for _, device := range cfg.Devices {
hash := md5.Sum([]byte(forHash + device + key))
if sign == strings.ToLower(hex.EncodeToString(hash[:])) {
valid = true
break
}
}
} else {
hash := md5.Sum([]byte(forHash + key))
valid = sign == strings.ToLower(hex.EncodeToString(hash[:]))
}
if len(device) > 0 && valid {
err = next(c)
if err != nil {
code = http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}
msg = http.StatusText(code)
} else {
code = c.Response().Status
}
} else {
code = http.StatusUnauthorized
msg = http.StatusText(code)
}
if len(msg) > 0 {
c.JSON(code, Result{Message: msg})
}
url := req.URL.String()
if ind := strings.IndexByte(url, '?'); ind >= 0 {
url = url[:ind]
}
out := fmt.Sprintf("%s,%s,%s,%d", url, remoteAddr, device, code)
cmd := c.Get("cmd")
if cmd != nil {
out += `,` + cmd.(string)
}
isError := c.Get("error")
if code != http.StatusOK || (isError != nil && isError.(bool)) {
golog.Warn(out)
} else {
golog.Info(out)
}
return err
}
}
func ping(c echo.Context) error {
return c.JSON(http.StatusOK, Result{
Version: Version,
DeviceOn: len(cfg.Devices) > 0,
})
}
func list(c echo.Context) error {
return c.JSON(http.StatusOK, Result{
Btns: cfg.Btns,
DefColor: cfg.DefColor,
DefIcon: cfg.DefIcon,
})
}
func run(c echo.Context) error {
var (
curDir string
err error
)
errRun := func(msg string) error {
c.Set("error", true)
return c.JSON(http.StatusOK, Result{
Message: msg,
})
}
key := c.QueryParam(`key`)
c.Set("error", false)
if iCmd, ok := cmds[key]; ok {
cmd := iCmd.Cmd
if len(iCmd.Params) > 0 {
cmd += ` ` + fmt.Sprint(iCmd.Params)
}
c.Set("cmd", cmd)
if len(iCmd.Dir) > 0 {
curDir, _ = os.Getwd()
if err = os.Chdir(iCmd.Dir); err != nil {
return errRun(fmt.Sprintf("Cannot set dir %s", iCmd.Dir))
}
defer func() {
os.Chdir(curDir)
}()
}
if err = exec.Command(iCmd.Cmd, iCmd.Params...).Start(); err != nil {
return errRun(fmt.Sprintf("Cannot run %s", iCmd.Cmd))
}
return c.JSON(http.StatusOK, Result{})
}
return errRun("Unknown command")
}
func RunServer() {
e := echo.New()
e.HideBanner = true
e.Use(Logger)
e.Use(md.Recover())
e.GET("/", ping)
e.GET("/list", list)
e.GET("/run", run)
if err := e.Start(fmt.Sprintf(":%d", cfg.Port)); err != nil {
golog.Fatal(err)
}
}