socks5.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. package plugin
  15. import (
  16. "io"
  17. "io/ioutil"
  18. "log"
  19. "net"
  20. frpNet "github.com/fatedier/frp/pkg/util/net"
  21. gosocks5 "github.com/armon/go-socks5"
  22. )
  23. const PluginSocks5 = "socks5"
  24. func init() {
  25. Register(PluginSocks5, NewSocks5Plugin)
  26. }
  27. type Socks5Plugin struct {
  28. Server *gosocks5.Server
  29. user string
  30. passwd string
  31. }
  32. func NewSocks5Plugin(params map[string]string) (p Plugin, err error) {
  33. user := params["plugin_user"]
  34. passwd := params["plugin_passwd"]
  35. cfg := &gosocks5.Config{
  36. Logger: log.New(ioutil.Discard, "", log.LstdFlags),
  37. }
  38. if user != "" || passwd != "" {
  39. cfg.Credentials = gosocks5.StaticCredentials(map[string]string{user: passwd})
  40. }
  41. sp := &Socks5Plugin{}
  42. sp.Server, err = gosocks5.New(cfg)
  43. p = sp
  44. return
  45. }
  46. func (sp *Socks5Plugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
  47. defer conn.Close()
  48. wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
  49. sp.Server.ServeConn(wrapConn)
  50. }
  51. func (sp *Socks5Plugin) Name() string {
  52. return PluginSocks5
  53. }
  54. func (sp *Socks5Plugin) Close() error {
  55. return nil
  56. }