| // |
| 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 |
| 15 | //go:build !frps |
| 16 |
| 17 | package client |
| 18 |
| 19 | import ( |
| 20 | "context" |
| 21 | "net/http" |
| 22 | "time" |
| 23 |
| 24 | "github.com/gorilla/mux" |
| 25 |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 28 | ) |
| 29 |
| 30 | func init() { |
| 31 | Register(v1.PluginStaticFile, NewStaticFilePlugin) |
| 32 | } |
| 33 |
| 34 | type StaticFilePlugin struct { |
| 35 | opts *v1.StaticFilePluginOptions |
| 36 |
| 37 | l *Listener |
| 38 | s *http.Server |
| 39 | } |
| 40 |
| 41 | func NewStaticFilePlugin(_ PluginContext, options v1.ClientPluginOptions) (Plugin, error) { |
| 42 | opts := options.(*v1.StaticFilePluginOptions) |
| 43 |
| 44 | listener := NewProxyListener() |
| 45 |
| 46 | sp := &StaticFilePlugin{ |
| 47 | opts: opts, |
| 48 |
| 49 | l: listener, |
| 50 | } |
| 51 | var prefix string |
| 52 | if opts.StripPrefix != "" { |
| 53 | prefix = "/" + opts.StripPrefix + "/" |
| 54 | } else { |
| 55 | prefix = "/" |
| 56 | } |
| 57 |
| 58 | router := mux.NewRouter() |
| 59 | router.Use(netpkg.NewHTTPAuthMiddleware(opts.HTTPUser, opts.HTTPPassword).SetAuthFailDelay(200 * time.Millisecond).Middleware) |
| 60 | router.PathPrefix(prefix).Handler(netpkg.MakeHTTPGzipHandler(http.StripPrefix(prefix, http.FileServer(http.Dir(opts.LocalPath))))).Methods("GET") |
| 61 | sp.s = &http.Server{ |
| 62 | Handler: router, |
| 63 | ReadHeaderTimeout: 60 * time.Second, |
| 64 | } |
| 65 | go func() { |
| 66 | _ = sp.s.Serve(listener) |
| 67 | }() |
| 68 | return sp, nil |
| 69 | } |
| 70 |
| 71 | func (sp *StaticFilePlugin) Handle(_ context.Context, connInfo *ConnectionInfo) { |
| 72 | wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn) |
| 73 | _ = sp.l.PutConn(wrapConn) |
| 74 | } |
| 75 |
| 76 | func (sp *StaticFilePlugin) Name() string { |
| 77 | return v1.PluginStaticFile |
| 78 | } |
| 79 |
| 80 | func (sp *StaticFilePlugin) Close() error { |
| 81 | sp.s.Close() |
| 82 | sp.l.Close() |
| 83 | return nil |
| 84 | } |