connection.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. type TLSSettings struct {
  23. AllowInsecure bool
  24. Certs []tls.Certificate
  25. }
  26. func (this *TLSSettings) GetTLSConfig() *tls.Config {
  27. config := &tls.Config{
  28. InsecureSkipVerify: this.AllowInsecure,
  29. }
  30. config.Certificates = this.Certs
  31. config.BuildNameToCertificate()
  32. return config
  33. }
  34. type StreamSettings struct {
  35. Type StreamConnectionType
  36. Security StreamSecurityType
  37. TLSSettings *TLSSettings
  38. }
  39. func (this *StreamSettings) IsCapableOf(streamType StreamConnectionType) bool {
  40. return (this.Type & streamType) == streamType
  41. }
  42. type Connection interface {
  43. net.Conn
  44. Reusable
  45. }
  46. type SysFd interface {
  47. SysFd() (int, error)
  48. }