1
0

dashboard.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package server
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "time"
  20. "github.com/fatedier/frp/assets"
  21. "github.com/fatedier/frp/g"
  22. frpNet "github.com/fatedier/frp/utils/net"
  23. "github.com/julienschmidt/httprouter"
  24. )
  25. var (
  26. httpServerReadTimeout = 10 * time.Second
  27. httpServerWriteTimeout = 10 * time.Second
  28. )
  29. func RunDashboardServer(addr string, port int) (err error) {
  30. // url router
  31. router := httprouter.New()
  32. user, passwd := g.GlbServerCfg.DashboardUser, g.GlbServerCfg.DashboardPwd
  33. // api, see dashboard_api.go
  34. router.GET("/api/serverinfo", frpNet.HttprouterBasicAuth(apiServerInfo, user, passwd))
  35. router.GET("/api/proxy/tcp/:name", frpNet.HttprouterBasicAuth(apiProxyTcpByName, user, passwd))
  36. router.GET("/api/proxy/udp/:name", frpNet.HttprouterBasicAuth(apiProxyUdpByName, user, passwd))
  37. router.GET("/api/proxy/http/:name", frpNet.HttprouterBasicAuth(apiProxyHttpByName, user, passwd))
  38. router.GET("/api/proxy/https/:name", frpNet.HttprouterBasicAuth(apiProxyHttpsByName, user, passwd))
  39. router.GET("/api/proxy/tcp", frpNet.HttprouterBasicAuth(apiProxyTcp, user, passwd))
  40. router.GET("/api/proxy/udp", frpNet.HttprouterBasicAuth(apiProxyUdp, user, passwd))
  41. router.GET("/api/proxy/http", frpNet.HttprouterBasicAuth(apiProxyHttp, user, passwd))
  42. router.GET("/api/proxy/https", frpNet.HttprouterBasicAuth(apiProxyHttps, user, passwd))
  43. router.GET("/api/proxy/traffic/:name", frpNet.HttprouterBasicAuth(apiProxyTraffic, user, passwd))
  44. // view
  45. router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
  46. router.Handler("GET", "/static/*filepath", frpNet.MakeHttpGzipHandler(
  47. frpNet.NewHttpBasicAuthWraper(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)), user, passwd)))
  48. router.HandlerFunc("GET", "/", frpNet.HttpBasicAuth(func(w http.ResponseWriter, r *http.Request) {
  49. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  50. }, user, passwd))
  51. address := fmt.Sprintf("%s:%d", addr, port)
  52. server := &http.Server{
  53. Addr: address,
  54. Handler: router,
  55. ReadTimeout: httpServerReadTimeout,
  56. WriteTimeout: httpServerWriteTimeout,
  57. }
  58. if address == "" {
  59. address = ":http"
  60. }
  61. ln, err := net.Listen("tcp", address)
  62. if err != nil {
  63. return err
  64. }
  65. go server.Serve(ln)
  66. return
  67. }