socks5.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. //go:build !frps
  15. package plugin
  16. import (
  17. "context"
  18. "io"
  19. "log"
  20. "net"
  21. gosocks5 "github.com/armon/go-socks5"
  22. v1 "github.com/fatedier/frp/pkg/config/v1"
  23. netpkg "github.com/fatedier/frp/pkg/util/net"
  24. )
  25. func init() {
  26. Register(v1.PluginSocks5, NewSocks5Plugin)
  27. }
  28. type Socks5Plugin struct {
  29. Server *gosocks5.Server
  30. }
  31. func NewSocks5Plugin(options v1.ClientPluginOptions) (p Plugin, err error) {
  32. opts := options.(*v1.Socks5PluginOptions)
  33. cfg := &gosocks5.Config{
  34. Logger: log.New(io.Discard, "", log.LstdFlags),
  35. }
  36. if opts.Username != "" || opts.Password != "" {
  37. cfg.Credentials = gosocks5.StaticCredentials(map[string]string{opts.Username: opts.Password})
  38. }
  39. sp := &Socks5Plugin{}
  40. sp.Server, err = gosocks5.New(cfg)
  41. p = sp
  42. return
  43. }
  44. func (sp *Socks5Plugin) Handle(_ context.Context, conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
  45. defer conn.Close()
  46. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  47. _ = sp.Server.ServeConn(wrapConn)
  48. }
  49. func (sp *Socks5Plugin) Name() string {
  50. return v1.PluginSocks5
  51. }
  52. func (sp *Socks5Plugin) Close() error {
  53. return nil
  54. }