ZBLOG

golang 编写 设置 IOS系统 代理服务器

由于 iOS 系统的网络请求走的是系统代理,所以我们需要设置 iOS 系统的代理服务器来实现全局代理。

  1. 首先,我们需要导入 MobileDevice.framework 和 libimobiledevice.dylib 两个库,用来连接 iOS 设备并执行操作。
  1. /*
  2. #cgo LDFLAGS: -framework MobileDevice -limobiledevice
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include "libimobiledevice/libimobiledevice.h"
  10. #include "libimobiledevice/lockdown.h"
  11. void print_error(char *msg, int code);
  12. void print_lockdownd_error(const char *msg, lockdownd_error_t err);
  13. void print_error(char *msg, int code) {
  14. fprintf(stderr, "%s failed: %d\n", msg, code);
  15. }
  16. void print_lockdownd_error(const char *msg, lockdownd_error_t err) {
  17. fprintf(stderr, "%s: %d\n", msg, err);
  18. }
  19. */
  20. import "C"
  1. 连接 iOS 设备
  1. func Connect() (C.lockdownd_client_t, error) {
  2. var client C.lockdownd_client_t
  3. var device C.idevice_t
  4. var err C.int
  5. // 初始化 idevice
  6. err = C.idevice_new(&device, nil)
  7. if err != C.IDEVICE_E_SUCCESS {
  8. return nil, fmt.Errorf("Failed to create device, error %d", err)
  9. }
  10. // 初始化 client
  11. err = C.lockdownd_client_new_with_handshake(device, &client, "golang")
  12. if err != C.LOCKDOWN_E_SUCCESS {
  13. C.idevice_free(device)
  14. return nil, fmt.Errorf("Failed to create client, error %d", err)
  15. }
  16. return client, nil
  17. }
  1. 设置代理服务器
  1. func SetProxyServer(client C.lockdownd_client_t, proxy string) error {
  2. var err C.lockdownd_error_t
  3. var dict C.plist_t
  4. // 创建 plist
  5. dict = C.plist_new_dict()
  6. if dict == nil {
  7. return errors.New("Failed to create plist")
  8. }
  9. // 添加本地化语言信息
  10. err = C.lockdownd_set_value(client, nil, "com.apple.international", dict)
  11. if err != C.LOCKDOWN_E_SUCCESS {
  12. C.plist_free(dict)
  13. return fmt.Errorf("Failed to set com.apple.international, error %d", err)
  14. }
  15. // 添加全局代理信息
  16. var proxyDict C.plist_t
  17. proxyDict = C.plist_new_dict()
  18. if proxyDict == nil {
  19. C.plist_free(dict)
  20. return errors.New("Failed to create proxyDict")
  21. }
  22. C.plist_dict_insert_item(proxyDict, "HTTPEnable", C.plist_new_bool(1))
  23. C.plist_dict_insert_item(proxyDict, "HTTPSEnable", C.plist_new_bool(1))
  24. C.plist_dict_insert_item(proxyDict, "HTTPProxy", C.plist_new_string(C.CString(proxy)))
  25. C.plist_dict_insert_item(proxyDict, "HTTPSPort", C.plist_new_int(8888))
  26. C.plist_dict_insert_item(proxyDict, "HTTPPort", C.plist_new_int(8888))
  27. C.plist_dict_insert_item(proxyDict, "ProxyAutoConfigEnable", C.plist_new_bool(0))
  28. C.plist_dict_insert_item(proxyDict, "ProxyAutoConfigURLString", C.plist_new_string(C.CString("")))
  29. C.plist_dict_insert_item(proxyDict, "ExcludeSimpleHostnames", C.plist_new_bool(0))
  30. C.plist_dict_insert_item(proxyDict, "FTPEnable", C.plist_new_bool(0))
  31. C.plist_dict_insert_item(proxyDict, "GopherEnable", C.plist_new_bool(0))
  32. C.plist_dict_insert_item(dict, "HTTPProxy", proxyDict)
  33. C.plist_dict_insert_item(dict, "HTTPSEnable", C.plist_new_bool(1))
  34. C.plist_dict_insert_item(dict, "HTTPEnable", C.plist_new_bool(1))
  35. C.plist_dict_insert_item(dict, "HTTPSProxy", proxyDict)
  36. // 发送 plist 到设备
  37. err = C.lockdownd_set_value(client, nil, "com.apple.network.proxy", dict)
  38. if err != C.LOCKDOWN_E_SUCCESS {
  39. C.plist_free(dict)
  40. return fmt.Errorf("Failed to set com.apple.network.proxy, error %d", err)
  41. }
  42. C.plist_free(dict)
  43. return nil
  44. }
  1. 调用函数设置代理服务器
  1. func main() {
  2. client, err := Connect()
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. defer C.idevice_free(client.device)
  7. defer C.lockdownd_client_free(client)
  8. err = SetProxyServer(client, "127.0.0.1:8888")
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. }

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=1381

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?