connection.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. )
  17. type StreamSecurityType int
  18. const (
  19. StreamSecurityTypeNone StreamSecurityType = 0
  20. StreamSecurityTypeTLS StreamSecurityType = 1
  21. )
  22. var (
  23. globalSessionCache = tls.NewLRUClientSessionCache(128)
  24. )
  25. type TLSSettings struct {
  26. AllowInsecure bool
  27. Certs []tls.Certificate
  28. }
  29. func (this *TLSSettings) GetTLSConfig() *tls.Config {
  30. config := &tls.Config{
  31. InsecureSkipVerify: this.AllowInsecure,
  32. ClientSessionCache: globalSessionCache,
  33. }
  34. config.Certificates = this.Certs
  35. config.BuildNameToCertificate()
  36. return config
  37. }
  38. type StreamSettings struct {
  39. Type StreamConnectionType
  40. Security StreamSecurityType
  41. TLSSettings *TLSSettings
  42. }
  43. func (this *StreamSettings) IsCapableOf(streamType StreamConnectionType) bool {
  44. return (this.Type & streamType) == streamType
  45. }
  46. type Connection interface {
  47. net.Conn
  48. Reusable
  49. }
  50. type SysFd interface {
  51. SysFd() (int, error)
  52. }