dashboard.go 2.3 KB
Newer Older
1
// Copyright 2017 fatedier, fatedier@gmail.com
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
18
	"net"
P
panhao 已提交
19
	"net/http"
20
	"time"
21

F
fatedier 已提交
22
	"github.com/fatedier/frp/assets"
F
fatedier 已提交
23
	frpNet "github.com/fatedier/frp/pkg/util/net"
24

F
fatedier 已提交
25
	"github.com/gorilla/mux"
26
	"github.com/prometheus/client_golang/prometheus/promhttp"
27 28
)

29 30 31 32
var (
	httpServerReadTimeout  = 10 * time.Second
	httpServerWriteTimeout = 10 * time.Second
)
P
panhao 已提交
33

Y
yuyulei 已提交
34
func (svr *Service) RunDashboardServer(address string) (err error) {
35
	// url router
F
fatedier 已提交
36
	router := mux.NewRouter()
37

38
	user, passwd := svr.cfg.DashboardUser, svr.cfg.DashboardPwd
F
fatedier 已提交
39
	router.Use(frpNet.NewHTTPAuthMiddleware(user, passwd).Middleware)
40

41 42 43 44 45
	// metrics
	if svr.cfg.EnablePrometheus {
		router.Handle("/metrics", promhttp.Handler())
	}

46
	// api, see dashboard_api.go
F
fatedier 已提交
47 48 49 50
	router.HandleFunc("/api/serverinfo", svr.APIServerInfo).Methods("GET")
	router.HandleFunc("/api/proxy/{type}", svr.APIProxyByType).Methods("GET")
	router.HandleFunc("/api/proxy/{type}/{name}", svr.APIProxyByTypeAndName).Methods("GET")
	router.HandleFunc("/api/traffic/{name}", svr.APIProxyTraffic).Methods("GET")
51

F
fatedier 已提交
52
	// view
F
fatedier 已提交
53
	router.Handle("/favicon.ico", http.FileServer(assets.FileSystem)).Methods("GET")
F
fatedier 已提交
54
	router.PathPrefix("/static/").Handler(frpNet.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))).Methods("GET")
55

F
fatedier 已提交
56
	router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
F
fatedier 已提交
57
		http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
F
fatedier 已提交
58
	})
59 60 61

	server := &http.Server{
		Addr:         address,
62
		Handler:      router,
63 64 65
		ReadTimeout:  httpServerReadTimeout,
		WriteTimeout: httpServerWriteTimeout,
	}
Y
yuyulei 已提交
66
	if address == "" || address == ":" {
67 68 69
		address = ":http"
	}
	ln, err := net.Listen("tcp", address)
P
panhao 已提交
70 71 72 73
	if err != nil {
		return err
	}

74 75
	go server.Serve(ln)
	return
P
panhao 已提交
76
}