|
@@ -0,0 +1,87 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+package plugin
|
|
|
+
|
|
|
+import (
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
+ "github.com/julienschmidt/httprouter"
|
|
|
+
|
|
|
+ frpNet "github.com/fatedier/frp/utils/net"
|
|
|
+)
|
|
|
+
|
|
|
+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 := httprouter.New()
|
|
|
+ router.Handler("GET", prefix+"*filepath", frpNet.MakeHttpGzipHandler(
|
|
|
+ frpNet.NewHttpBasicAuthWraper(http.StripPrefix(prefix, http.FileServer(http.Dir(localPath))), httpUser, httpPasswd)))
|
|
|
+ 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
|
|
|
+}
|