utls.go 872 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package webtunnel
  2. import (
  3. "errors"
  4. "net"
  5. utls "github.com/refraction-networking/utls"
  6. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/common/utlsutil"
  7. )
  8. type uTLSConfig struct {
  9. ServerName string
  10. uTLSFingerprint string
  11. }
  12. func newUTLSTransport(config *uTLSConfig) (uTLSTransport, error) {
  13. return uTLSTransport{kind: "utls", serverName: config.ServerName, uTLSFingerprint: config.uTLSFingerprint}, nil
  14. }
  15. type uTLSTransport struct {
  16. kind string
  17. serverName string
  18. uTLSFingerprint string
  19. }
  20. func (t *uTLSTransport) Client(conn net.Conn) (net.Conn, error) {
  21. switch t.kind {
  22. case "utls":
  23. fp, err := utlsutil.ParseClientHelloID(t.uTLSFingerprint)
  24. if err != nil {
  25. return nil, err
  26. }
  27. conf := &utls.Config{ServerName: t.serverName}
  28. return utls.UClient(conn, conf, *fp), nil
  29. }
  30. return nil, errors.New("unknown kind")
  31. }