connection.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package internet
  2. import (
  3. "crypto/tls"
  4. "net"
  5. )
  6. type ConnectionHandler func(Connection)
  7. type Reusable interface {
  8. Reusable() bool
  9. SetReusable(reuse bool)
  10. }
  11. type StreamConnectionType int
  12. const (
  13. StreamConnectionTypeRawTCP StreamConnectionType = 1
  14. StreamConnectionTypeTCP StreamConnectionType = 2
  15. StreamConnectionTypeKCP StreamConnectionType = 4
  16. StreamConnectionTypeWebSocket StreamConnectionType = 8
  17. )
  18. type StreamSecurityType int
  19. const (
  20. StreamSecurityTypeNone StreamSecurityType = 0
  21. StreamSecurityTypeTLS StreamSecurityType = 1
  22. )
  23. var (
  24. globalSessionCache = tls.NewLRUClientSessionCache(128)
  25. )
  26. type TLSSettings struct {
  27. AllowInsecure bool
  28. Certs []tls.Certificate
  29. }
  30. func (this *TLSSettings) GetTLSConfig() *tls.Config {
  31. config := &tls.Config{
  32. ClientSessionCache: globalSessionCache,
  33. }
  34. if this == nil {
  35. return config
  36. }
  37. config.InsecureSkipVerify = this.AllowInsecure
  38. config.Certificates = this.Certs
  39. config.BuildNameToCertificate()
  40. return config
  41. }
  42. type StreamSettings struct {
  43. Type StreamConnectionType
  44. Security StreamSecurityType
  45. TLSSettings *TLSSettings
  46. }
  47. func (this *StreamSettings) IsCapableOf(streamType StreamConnectionType) bool {
  48. return (this.Type & streamType) == streamType
  49. }
  50. type Connection interface {
  51. net.Conn
  52. Reusable
  53. }
  54. type SysFd interface {
  55. SysFd() (int, error)
  56. }