This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (120 loc) · 3.21 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net"
"net/http"
"os"
"os/exec"
)
var (
USER = "admin"
PASSWORD = "passwd"
IPADDRESS = "192.168.0.1"
MAC = "CF:A8:59:57:67:A5"
TOKEN = "hhTp5eUSsc7iS5"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
var temp string
if temp = os.Getenv("USER"); temp != "" {
USER = temp
}
if temp = os.Getenv("PASSWORD"); temp != "" {
PASSWORD = temp
}
if temp = os.Getenv("IPADDRESS"); temp != "" {
IPADDRESS = temp
}
if temp = os.Getenv("MAC"); temp != "" {
MAC = temp
}
}
func main() {
log.Infof("Run models...")
log.Infof("Server Start: http://%v:33659\n", getClientIp())
r := gin.Default()
r.NoRoute(func(c *gin.Context) {
c.String(http.StatusBadGateway, "Server error!")
})
cli := r.Use(Auth())
cli.GET("/power", func(c *gin.Context) {
typeTmp := c.Query("type")
if typeTmp == "" {
c.String(http.StatusBadGateway, "Server error!")
return
}
cmd := exec.Command("ipmitool", "-I", "lan", "-U", USER, "-P", PASSWORD, "-H", IPADDRESS, "chassis", "power", typeTmp)
out, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("power %v failed with %s\n", typeTmp, err)
c.String(http.StatusBadRequest, fmt.Sprintf("power %v failed with %v", typeTmp, err))
return
}
c.String(http.StatusOK, fmt.Sprintf("power %v success with %v", typeTmp, string(out)))
})
cli.GET("/powerRouter", func(c *gin.Context) {
typeTmp := c.Query("type")
if typeTmp == "" {
c.String(http.StatusBadGateway, "Server error!")
return
}
if typeTmp != "on" {
c.String(http.StatusBadGateway, "Server error!")
return
}
cmd := exec.Command("wakeonlan", MAC)
_, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("power %v failed with %s\n", typeTmp, err)
c.String(http.StatusBadRequest, fmt.Sprintf("power %v failed with %v", typeTmp, err))
return
}
c.String(http.StatusOK, "powerRouter on success")
})
r.GET("/fan", func(c *gin.Context) {
// ipmitool -I lan -U USER -P PASSWD -H IPADDRESS raw 0x30 0x70 0x66 0x01 0x01 0x20
cmd := exec.Command("ipmitool", "-I", "lan", "-U", USER, "-P", PASSWORD, "-H", IPADDRESS, "raw", "0x30", "0x70", "0x66", "0x01", "0x01", "0x20")
out, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("set fan failed with %s\n", err)
c.String(http.StatusBadRequest, fmt.Sprintf("set fan failed with %v", err))
return
}
c.String(http.StatusOK, fmt.Sprintf("set fan success with %v", string(out)))
})
if err := r.Run(":33659"); err != nil {
log.Errorf("Run Error: [%v]", err)
return
}
}
func getClientIp() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Errorf("获取本机 IP 失败: %v", err)
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func Auth() gin.HandlerFunc {
return func(context *gin.Context) {
tmp := context.Request.URL.Query().Get("token")
if tmp == "" || tmp != TOKEN {
context.String(http.StatusBadGateway, "Server error!")
context.Abort()
return
}
context.Next()
}
}