main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "fmt"
  4. "git.swzry.com/zry/go-hhc-cli/hhc_mangekyo"
  5. "git.swzry.com/zry/go-hhc-cli/hhc_telws"
  6. "github.com/gin-gonic/gin"
  7. "github.com/gorilla/websocket"
  8. "github.com/tjfoc/gmsm/sm2"
  9. "net/http"
  10. "os"
  11. "time"
  12. )
  13. var ListenAddress string
  14. var TestSM2KeyPair *sm2.PrivateKey
  15. var upGrader = websocket.Upgrader{
  16. CheckOrigin: func(r *http.Request) bool {
  17. return true
  18. },
  19. }
  20. func main() {
  21. //runtime.Breakpoint()
  22. ListenAddress = os.Getenv("listen")
  23. if ListenAddress == "" {
  24. fmt.Println("No environment variable 'listen', use default value ':9090'")
  25. ListenAddress = ":9090"
  26. }
  27. k, err := sm2.GenerateKey()
  28. if err != nil {
  29. fmt.Println("Failed Generate SM2 Key:", err)
  30. return
  31. }
  32. TestSM2KeyPair = k
  33. r := gin.Default()
  34. r.GET("/api/app-config.satori", appConfig)
  35. telwsGrp := r.Group("/api/telws/")
  36. muh := NewMUH()
  37. mhf := hhc_mangekyo.NewMangekyoHandlerFactory(muh)
  38. cbh := hhc_telws.NewCliBackendHandler(telwsGrp, mhf)
  39. err = cbh.InitSecure(time.Minute * 10)
  40. if err != nil {
  41. fmt.Println("Failed Init Telws Handler:", err)
  42. return
  43. }
  44. sah := hhc_telws.NewTelwsAuthSimpleAAA()
  45. sah.SimpleAAAAddSimple("admin", "114514", 4)
  46. cbh.AddAuthHandler(sah)
  47. r.Run(ListenAddress)
  48. }
  49. func appConfig(ctx *gin.Context) {
  50. ctx.JSON(200, gin.H{
  51. "title": "Hypothetical Swtich",
  52. "appBarColor": "#1c2c61",
  53. "telwsUrl": "/api/telws/",
  54. "projectName": "Hypothetical Switch",
  55. "projectDescription": "A hypothetical switch CLI interface for HHC_CLI demo.",
  56. })
  57. }