2023-09-02 11:47:53 +08:00
|
|
|
/*
|
|
|
|
* @Author: giaogiao giaogiao
|
|
|
|
* @Date: 2023-08-28 10:24:38
|
|
|
|
* @LastEditors: giaogiao giaogiao
|
|
|
|
* @LastEditTime: 2023-09-01 16:40:52
|
|
|
|
* @FilePath: \goweb\main.go
|
|
|
|
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2023-09-02 16:05:31 +08:00
|
|
|
"goweb/controller"
|
2023-09-02 11:47:53 +08:00
|
|
|
"goweb/wol"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed app
|
|
|
|
var embededFiles embed.FS
|
|
|
|
|
|
|
|
func getFileSystem(useOS bool) http.FileSystem {
|
|
|
|
if useOS {
|
|
|
|
log.Print("using live mode")
|
|
|
|
return http.FS(os.DirFS("app"))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("using embed mode")
|
|
|
|
fsys, err := fs.Sub(embededFiles, "app")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.FS(fsys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func get_send_start(c echo.Context) error {
|
|
|
|
mac := c.Param("mac")
|
|
|
|
println("开始发送wol 地址为" + mac)
|
|
|
|
a, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if int(a[i].Flags) > 50 {
|
|
|
|
fmt.Printf("正在往 %s 接口发送\n", a[i].Name)
|
|
|
|
wol.WakespecifyInterfac(mac, "255.255.255.255", "9", a[i].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "已经发送请求MAC :"+mac)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func hello(c echo.Context) error {
|
|
|
|
type Data struct {
|
|
|
|
Mac string `json:"mac"`
|
|
|
|
}
|
|
|
|
type TooLTT struct {
|
|
|
|
Method string `json:"method"`
|
|
|
|
Data Data `json:"data"`
|
2023-09-05 17:53:38 +08:00
|
|
|
Uuid string `json:"uuid"`
|
2023-09-02 11:47:53 +08:00
|
|
|
}
|
|
|
|
websocket.Handler(func(ws *websocket.Conn) {
|
|
|
|
defer ws.Close()
|
|
|
|
var wsValue TooLTT
|
|
|
|
for {
|
|
|
|
// Write
|
|
|
|
|
|
|
|
// timeUnix := time.Now().Unix()
|
|
|
|
// output, _ := json.Marshal(&wsValue)
|
|
|
|
// err := websocket.Message.Send(ws, "Hello, Client!"+string(output)+" time is :"+strconv.FormatInt(timeUnix, 10))
|
|
|
|
// if err != nil {
|
|
|
|
// c.Logger().Error(err)
|
|
|
|
// }
|
|
|
|
// Read
|
|
|
|
msg := ""
|
|
|
|
err := websocket.Message.Receive(ws, &msg)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
json.Unmarshal([]byte(msg), &wsValue)
|
|
|
|
switch {
|
|
|
|
case wsValue.Method == "start":
|
2023-09-05 17:53:38 +08:00
|
|
|
controller.Send_start(wsValue.Data.Mac, &ws, wsValue.Uuid)
|
2023-09-02 16:05:31 +08:00
|
|
|
case wsValue.Method == "getall":
|
2023-09-05 17:53:38 +08:00
|
|
|
controller.Getall(&ws, wsValue.Uuid)
|
2023-09-02 11:47:53 +08:00
|
|
|
default:
|
|
|
|
fmt.Printf("error")
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", msg)
|
|
|
|
|
|
|
|
}
|
|
|
|
}).ServeHTTP(c.Response(), c.Request())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
|
|
|
useOS := len(os.Args) > 1 && os.Args[1] == "live"
|
|
|
|
assetHandler := http.FileServer(getFileSystem(useOS))
|
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}}))
|
|
|
|
e.GET("/", echo.WrapHandler(assetHandler))
|
|
|
|
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
|
|
|
|
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
e.GET("/ws", hello)
|
|
|
|
e.GET("/mac/:mac", get_send_start)
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
|
|
}
|