Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

求助HTTPS代理怎么设置呢? #288

Closed
qinghuawen opened this issue Feb 13, 2020 · 29 comments
Closed

求助HTTPS代理怎么设置呢? #288

qinghuawen opened this issue Feb 13, 2020 · 29 comments

Comments

@qinghuawen
Copy link

大佬,我想通过network extension实现简单的https代理,就像pc上浏览器设置https代理一样。可否贴一些demo代码呢?

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020

@zhuhaow zhuhaow closed this as completed Feb 14, 2020
@qinghuawen
Copy link
Author

感谢您的回复,这个我试过,但好像不起作用,是否需要在PacketTunnelProvide里面做别的处理?
以下是我的代码。运行起来后,IP并不是https代理服务器的IP.

PacketTunnelProvider.m
- (void)startTunnelWithOptions:(NSDictionary *)options completionHandler:(void (^)(NSError *))completionHandler {
    NEIPv4Settings *ipv4Settings = [[NEIPv4Settings alloc] initWithAddresses:@[@"192.0.2.1"] subnetMasks:@[@"255.255.255.0"]];
    
    NSArray *dnsServers = @[@"8.8.8.8"];
    ipv4Settings.includedRoutes = @[[NEIPv4Route defaultRoute]];
    NEPacketTunnelNetworkSettings *settings = [[NEPacketTunnelNetworkSettings alloc] initWithTunnelRemoteAddress:@"192.0.2.2"];
    settings.IPv4Settings = ipv4Settings;
    settings.MTU = @(1500);
    NEProxySettings* proxySettings = [[NEProxySettings alloc] init];
    NSInteger proxyServerPort = 443;
    NSString *proxyServerName = @"xxxx.xxxx.xxxx";
    proxySettings.HTTPSEnabled = YES;
    proxySettings.HTTPSServer = [[NEProxyServer alloc] initWithAddress:proxyServerName port:proxyServerPort];
    proxySettings.autoProxyConfigurationEnabled = NO;
    proxySettings.excludeSimpleHostnames = YES;
    settings.proxySettings = proxySettings;
    NEDNSSettings *dnsSettings = [[NEDNSSettings alloc] initWithServers:dnsServers];
    settings.DNSSettings = dnsSettings;
    [self setTunnelNetworkSettings:settings completionHandler:^(NSError * _Nullable error) {
        if (error) {
            if (completionHandler) {
                completionHandler(error);
            }
        }else{
            if (completionHandler) {
                completionHandler(nil);
            }
        }
    }];
}

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

明白了,您的源码demo里面有这个转换处理吗?感激不尽

@qinghuawen
Copy link
Author

我想的就是填入一个https代理服务器的地址,连接后,网络请求通过代理发出,IP地址也会变成代理服务器的IP地址

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

qinghuawen commented Feb 14, 2020

用了PAC之后是不是可以不需要用NEKit的HttpsAdapter呢,只需要用我原来的代码,也就是NEProxySettings就可以了达到我的目标吗?

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

再次感谢大佬,我试试

@qinghuawen
Copy link
Author

您好,按照您的建议试了一下,连接上之后还是无网络,我的代理服务器肯定是正常的,因为PC浏览器可以正常使用代理。以下是代码片段。感觉我就差最后一步了。。

 NEPacketTunnelNetworkSettings *settings = [[NEPacketTunnelNetworkSettings alloc] initWithTunnelRemoteAddress:@"192.0.2.2"];
    settings.IPv4Settings = ipv4Settings;
    settings.MTU = @(1500);
    NEProxySettings* proxySettings = [[NEProxySettings alloc] init];
    NSInteger proxyServerPort = 443;
    NSString *proxyServerName = @"example.myproxy.com";
    proxySettings.HTTPSEnabled = YES;
    proxySettings.HTTPSServer = [[NEProxyServer alloc] initWithAddress:proxyServerName port:proxyServerPort];
    proxySettings.autoProxyConfigurationEnabled = YES;
    proxySettings.proxyAutoConfigurationURL = [NSURL URLWithString:@"https://example.mypac.com/pac.txt"];
    //proxySettings.excludeSimpleHostnames = YES;
    settings.proxySettings = proxySettings;

其中https://example.mypac.com/pac.txt的内容如下,格式应该也没问题。

function FindProxyForURL(url, host) {
    return "HTTPS example.myproxy.com:443"
}

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

您好,去掉httpsserver相关后,就只剩以下这几行代码了,好像还是有问题

NEProxySettings* proxySettings = [[NEProxySettings alloc] init];
proxySettings.autoProxyConfigurationEnabled = YES;
proxySettings.HTTPSEnabled = YES;
proxySettings.proxyAutoConfigurationURL = [NSURL 
URLWithString:@"https://example.mypac.com/pac.txt"];
settings.proxySettings = proxySettings;

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

您好,去掉HTTPSEnabled之后,就只剩与PAC相关的代码了。似乎还是没有网络。有没有可能是其他的比如ipv4setting等设置问题?

NEProxySettings* proxySettings = [[NEProxySettings alloc] init];
proxySettings.autoProxyConfigurationEnabled = YES;
proxySettings.proxyAutoConfigurationURL = [NSURL
URLWithString:@"https://example.mypac.com/pac.txt"];
settings.proxySettings = proxySettings;

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

您好,defaultRoute去掉后,网络正常,但是IP是本机IP,不是代理服务器的IP

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

您好,matchDomains也去掉了。现在整体代码如下:

- (void)startTunnelWithOptions:(NSDictionary *)options completionHandler:(void (^)(NSError *))completionHandler
{
    NEIPv4Settings *ipv4Settings = [[NEIPv4Settings alloc] initWithAddresses:@[@"192.0.2.1"] subnetMasks:@[@"255.255.255.0"]];

    NSArray *dnsServers = @[@"8.8.8.8"];
    NEPacketTunnelNetworkSettings *settings = [[NEPacketTunnelNetworkSettings alloc] initWithTunnelRemoteAddress:@"192.0.2.2"];
    settings.IPv4Settings = ipv4Settings;
    settings.MTU = @(1500);
    NEProxySettings* proxySettings = [[NEProxySettings alloc] init];
    proxySettings.autoProxyConfigurationEnabled = YES;
proxySettings.proxyAutoConfigurationURL = [NSURL
URLWithString:@"https://example.mypac.com/pac.txt"];
    settings.proxySettings = proxySettings;
    NEDNSSettings *dnsSettings = [[NEDNSSettings alloc] initWithServers:dnsServers];
    settings.DNSSettings = dnsSettings;
    [self setTunnelNetworkSettings:settings completionHandler:^(NSError * _Nullable error) {
        if (error) {
            if (completionHandler) {
                completionHandler(error);
            }
        }else{
            if (completionHandler) {
                completionHandler(nil);
            }
        }
    }];
}

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

所以还是需要借助您这个httpAdapter吗?

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020

@qinghuawen
Copy link
Author

您好,之前没有写matchDomains这个参数,刚才加上了设置proxySettings.matchDomains = [""],与刚才一样的现象,有网络但IP是本机IP

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020

你怎么测的ip

@qinghuawen
Copy link
Author

就是在safari里面打开测IP网站,比如百度搜索IP.

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020

pac服务器访问日志有记录么

@qinghuawen
Copy link
Author

您好,我现在没权限访问pac服务器,但我直接把js脚本写到ios代理里了。但是这样也是不行的。 :(

proxySettings.proxyAutoConfigurationJavaScript = @"function FindProxyForURL(url, host) { return \"HTTPS example.myproxy.com:443\"}";

@zhuhaow
Copy link
Owner

zhuhaow commented Feb 14, 2020 via email

@qinghuawen
Copy link
Author

好的,不好意思打扰您这么久,网上这方面的资料确实太少了。我再自己研究一下,非常感谢!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants