connection.go 1.2 KB

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