12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package plugin
- import (
- "io"
- "net/http"
- frpNet "github.com/fatedier/frp/utils/net"
- "github.com/gorilla/mux"
- )
- const PluginStaticFile = "static_file"
- func init() {
- Register(PluginStaticFile, NewStaticFilePlugin)
- }
- type StaticFilePlugin struct {
- localPath string
- stripPrefix string
- httpUser string
- httpPasswd string
- l *Listener
- s *http.Server
- }
- func NewStaticFilePlugin(params map[string]string) (Plugin, error) {
- localPath := params["plugin_local_path"]
- stripPrefix := params["plugin_strip_prefix"]
- httpUser := params["plugin_http_user"]
- httpPasswd := params["plugin_http_passwd"]
- listener := NewProxyListener()
- sp := &StaticFilePlugin{
- localPath: localPath,
- stripPrefix: stripPrefix,
- httpUser: httpUser,
- httpPasswd: httpPasswd,
- l: listener,
- }
- var prefix string
- if stripPrefix != "" {
- prefix = "/" + stripPrefix + "/"
- } else {
- prefix = "/"
- }
- router := mux.NewRouter()
- router.Use(frpNet.NewHttpAuthMiddleware(httpUser, httpPasswd).Middleware)
- router.PathPrefix(prefix).Handler(frpNet.MakeHttpGzipHandler(http.StripPrefix(prefix, http.FileServer(http.Dir(localPath))))).Methods("GET")
- sp.s = &http.Server{
- Handler: router,
- }
- go sp.s.Serve(listener)
- return sp, nil
- }
- func (sp *StaticFilePlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
- wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
- sp.l.PutConn(wrapConn)
- }
- func (sp *StaticFilePlugin) Name() string {
- return PluginStaticFile
- }
- func (sp *StaticFilePlugin) Close() error {
- sp.s.Close()
- sp.l.Close()
- return nil
- }
|