config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package tls
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "github.com/sagernet/sing-box/adapter"
  7. E "github.com/sagernet/sing/common/exceptions"
  8. )
  9. type (
  10. STDConfig = tls.Config
  11. STDConn = tls.Conn
  12. ConnectionState = tls.ConnectionState
  13. )
  14. type Config interface {
  15. ServerName() string
  16. SetServerName(serverName string)
  17. NextProtos() []string
  18. SetNextProtos(nextProto []string)
  19. Config() (*STDConfig, error)
  20. Client(conn net.Conn) (Conn, error)
  21. Clone() Config
  22. }
  23. type ConfigWithSessionIDGenerator interface {
  24. SetSessionIDGenerator(generator func(clientHello []byte, sessionID []byte) error)
  25. }
  26. type ServerConfig interface {
  27. Config
  28. adapter.Service
  29. Server(conn net.Conn) (Conn, error)
  30. }
  31. type ServerConfigCompat interface {
  32. ServerConfig
  33. ServerHandshake(ctx context.Context, conn net.Conn) (Conn, error)
  34. }
  35. type Conn interface {
  36. net.Conn
  37. HandshakeContext(ctx context.Context) error
  38. ConnectionState() ConnectionState
  39. }
  40. func ParseTLSVersion(version string) (uint16, error) {
  41. switch version {
  42. case "1.0":
  43. return tls.VersionTLS10, nil
  44. case "1.1":
  45. return tls.VersionTLS11, nil
  46. case "1.2":
  47. return tls.VersionTLS12, nil
  48. case "1.3":
  49. return tls.VersionTLS13, nil
  50. default:
  51. return 0, E.New("unknown tls version:", version)
  52. }
  53. }