Bubble android client. Fork of https://git.zx2c4.com/wireguard-android/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 line
2.3 KiB

  1. package main
  2. // #cgo LDFLAGS: -llog
  3. // #include <android/log.h>
  4. import "C"
  5. import (
  6. "bufio"
  7. "io/ioutil"
  8. "log"
  9. "math"
  10. "os"
  11. "strings"
  12. )
  13. type AndroidLogger struct {
  14. level C.int
  15. interfaceName string
  16. }
  17. func (l AndroidLogger) Write(p []byte) (int, error) {
  18. C.__android_log_write(l.level, C.CString("WireGuard/GoBackend/"+l.interfaceName), C.CString(string(p)))
  19. return len(p), nil
  20. }
  21. var tunnelHandles map[int32]*Device
  22. func init() {
  23. tunnelHandles = make(map[int32]*Device)
  24. }
  25. //export wgTurnOn
  26. func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 {
  27. interfaceName := string([]byte(ifnameRef))
  28. logger := &Logger{
  29. Debug: log.New(&AndroidLogger{level: C.ANDROID_LOG_DEBUG, interfaceName: interfaceName}, "", 0),
  30. Info: log.New(&AndroidLogger{level: C.ANDROID_LOG_INFO, interfaceName: interfaceName}, "", 0),
  31. Error: log.New(&AndroidLogger{level: C.ANDROID_LOG_ERROR, interfaceName: interfaceName}, "", 0),
  32. }
  33. logger.Debug.Println("Debug log enabled")
  34. tun := &NativeTun{
  35. fd: os.NewFile(uintptr(tun_fd), ""),
  36. events: make(chan TUNEvent, 5),
  37. errors: make(chan error, 5),
  38. nopi: true,
  39. }
  40. device := NewDevice(tun, logger)
  41. device.tun.mtu = DefaultMTU //TODO: make dynamic
  42. bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
  43. setError := ipcSetOperation(device, bufferedSettings)
  44. if setError != nil {
  45. logger.Debug.Println(setError)
  46. return -1
  47. }
  48. device.Up()
  49. logger.Info.Println("Device started")
  50. var i int32
  51. for i = 0; i < math.MaxInt32; i++ {
  52. if _, exists := tunnelHandles[i]; !exists {
  53. break
  54. }
  55. }
  56. if i == math.MaxInt32 {
  57. return -1
  58. }
  59. tunnelHandles[i] = device
  60. return i
  61. }
  62. //export wgTurnOff
  63. func wgTurnOff(tunnelHandle int32) {
  64. device, ok := tunnelHandles[tunnelHandle]
  65. if !ok {
  66. return
  67. }
  68. delete(tunnelHandles, tunnelHandle)
  69. device.Close()
  70. }
  71. //export wgGetSocketV4
  72. func wgGetSocketV4(tunnelHandle int32) int32 {
  73. device, ok := tunnelHandles[tunnelHandle]
  74. if !ok {
  75. return -1
  76. }
  77. native, ok := device.net.bind.(NativeBind)
  78. if !ok {
  79. return -1
  80. }
  81. return int32(native.sock4)
  82. }
  83. //export wgGetSocketV6
  84. func wgGetSocketV6(tunnelHandle int32) int32 {
  85. device, ok := tunnelHandles[tunnelHandle]
  86. if !ok {
  87. return -1
  88. }
  89. native, ok := device.net.bind.(NativeBind)
  90. if !ok {
  91. return -1
  92. }
  93. return int32(native.sock6)
  94. }
  95. func main() {}