connection.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. InsecureSkipVerify: this.AllowInsecure,
  33. ClientSessionCache: globalSessionCache,
  34. }
  35. config.Certificates = this.Certs
  36. config.BuildNameToCertificate()
  37. return config
  38. }
  39. type StreamSettings struct {
  40. Type StreamConnectionType
  41. Security StreamSecurityType
  42. TLSSettings *TLSSettings
  43. }
  44. func (this *StreamSettings) IsCapableOf(streamType StreamConnectionType) bool {
  45. return (this.Type & streamType) == streamType
  46. }
  47. type Connection interface {
  48. net.Conn
  49. Reusable
  50. }
  51. type SysFd interface {
  52. SysFd() (int, error)
  53. }