123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package server
- import (
- "fmt"
- "net"
- "net/http"
- "time"
- "github.com/fatedier/frp/assets"
- "github.com/fatedier/frp/models/config"
- "github.com/julienschmidt/httprouter"
- )
- var (
- httpServerReadTimeout = 10 * time.Second
- httpServerWriteTimeout = 10 * time.Second
- )
- func RunDashboardServer(addr string, port int64) (err error) {
-
- router := httprouter.New()
-
- router.GET("/api/serverinfo", httprouterBasicAuth(apiServerInfo))
- router.GET("/api/proxy/tcp", httprouterBasicAuth(apiProxyTcp))
- router.GET("/api/proxy/udp", httprouterBasicAuth(apiProxyUdp))
- router.GET("/api/proxy/http", httprouterBasicAuth(apiProxyHttp))
- router.GET("/api/proxy/https", httprouterBasicAuth(apiProxyHttps))
- router.GET("/api/proxy/traffic/:name", httprouterBasicAuth(apiProxyTraffic))
-
- router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
- router.Handler("GET", "/static/*filepath", basicAuthWraper(http.StripPrefix("/static/", http.FileServer(assets.FileSystem))))
- router.HandlerFunc("GET", "/", basicAuth(func(w http.ResponseWriter, r *http.Request) {
- http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
- }))
- address := fmt.Sprintf("%s:%d", addr, port)
- server := &http.Server{
- Addr: address,
- Handler: router,
- ReadTimeout: httpServerReadTimeout,
- WriteTimeout: httpServerWriteTimeout,
- }
- if address == "" {
- address = ":http"
- }
- ln, err := net.Listen("tcp", address)
- if err != nil {
- return err
- }
- go server.Serve(ln)
- return
- }
- func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
- for _, m := range middleware {
- h = m(h)
- }
- return h
- }
- type AuthWraper struct {
- h http.Handler
- user string
- passwd string
- }
- func (aw *AuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- user, passwd, hasAuth := r.BasicAuth()
- if hasAuth && user == aw.user || passwd == aw.passwd {
- aw.h.ServeHTTP(w, r)
- } else {
- w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
- http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
- }
- }
- func basicAuthWraper(h http.Handler) http.Handler {
- return &AuthWraper{
- h: h,
- user: config.ServerCommonCfg.DashboardUser,
- passwd: config.ServerCommonCfg.DashboardPwd,
- }
- }
- func basicAuth(h http.HandlerFunc) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- user, passwd, hasAuth := r.BasicAuth()
- if hasAuth && user == config.ServerCommonCfg.DashboardUser || passwd == config.ServerCommonCfg.DashboardPwd {
- h.ServeHTTP(w, r)
- } else {
- w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
- http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
- }
- }
- }
- func httprouterBasicAuth(h httprouter.Handle) httprouter.Handle {
- return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- user, passwd, hasAuth := r.BasicAuth()
- if hasAuth && user == config.ServerCommonCfg.DashboardUser || passwd == config.ServerCommonCfg.DashboardPwd {
- h(w, r, ps)
- } else {
- w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
- http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
- }
- }
- }
|