tun.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package libbox
  2. import (
  3. "io"
  4. "net/netip"
  5. "os"
  6. "github.com/sagernet/sing-tun"
  7. "github.com/sagernet/sing/common"
  8. E "github.com/sagernet/sing/common/exceptions"
  9. )
  10. type TunOptions interface {
  11. GetInet4Address() RoutePrefixIterator
  12. GetInet6Address() RoutePrefixIterator
  13. GetDNSServerAddress() (string, error)
  14. GetMTU() int32
  15. GetAutoRoute() bool
  16. GetStrictRoute() bool
  17. GetInet4RouteAddress() RoutePrefixIterator
  18. GetInet6RouteAddress() RoutePrefixIterator
  19. GetIncludePackage() StringIterator
  20. GetExcludePackage() StringIterator
  21. }
  22. type RoutePrefix struct {
  23. Address string
  24. Prefix int32
  25. }
  26. type RoutePrefixIterator interface {
  27. Next() *RoutePrefix
  28. HasNext() bool
  29. }
  30. func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator {
  31. return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix {
  32. return &RoutePrefix{
  33. Address: prefix.Addr().String(),
  34. Prefix: int32(prefix.Bits()),
  35. }
  36. }))
  37. }
  38. var _ TunOptions = (*tunOptions)(nil)
  39. type tunOptions tun.Options
  40. func (o *tunOptions) GetInet4Address() RoutePrefixIterator {
  41. return mapRoutePrefix(o.Inet4Address)
  42. }
  43. func (o *tunOptions) GetInet6Address() RoutePrefixIterator {
  44. return mapRoutePrefix(o.Inet6Address)
  45. }
  46. func (o *tunOptions) GetDNSServerAddress() (string, error) {
  47. if len(o.Inet4Address) == 0 || o.Inet4Address[0].Bits() == 32 {
  48. return "", E.New("need one more IPv4 address for DNS hijacking")
  49. }
  50. return o.Inet4Address[0].Addr().Next().String(), nil
  51. }
  52. func (o *tunOptions) GetMTU() int32 {
  53. return int32(o.MTU)
  54. }
  55. func (o *tunOptions) GetAutoRoute() bool {
  56. return o.AutoRoute
  57. }
  58. func (o *tunOptions) GetStrictRoute() bool {
  59. return o.StrictRoute
  60. }
  61. func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator {
  62. return mapRoutePrefix(o.Inet4RouteAddress)
  63. }
  64. func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator {
  65. return mapRoutePrefix(o.Inet6RouteAddress)
  66. }
  67. func (o *tunOptions) GetIncludePackage() StringIterator {
  68. return newIterator(o.IncludePackage)
  69. }
  70. func (o *tunOptions) GetExcludePackage() StringIterator {
  71. return newIterator(o.ExcludePackage)
  72. }
  73. type nativeTun struct {
  74. tunFd int
  75. tunFile *os.File
  76. tunMTU uint32
  77. closer io.Closer
  78. }
  79. func (t *nativeTun) Read(p []byte) (n int, err error) {
  80. return t.tunFile.Read(p)
  81. }
  82. func (t *nativeTun) Write(p []byte) (n int, err error) {
  83. return t.tunFile.Write(p)
  84. }
  85. func (t *nativeTun) Close() error {
  86. return t.closer.Close()
  87. }