AuthHttpProxyServer.java
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.github.monkeywie.proxyee;
import com.github.monkeywie.proxyee.intercept.HttpProxyIntercept;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptInitializer;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptPipeline;
import com.github.monkeywie.proxyee.server.HttpProxyServer;
import com.github.monkeywie.proxyee.server.HttpProxyServerConfig;
import com.github.monkeywie.proxyee.server.auth.BasicHttpProxyAuthenticationProvider;
import com.github.monkeywie.proxyee.server.auth.HttpAuthContext;
import com.github.monkeywie.proxyee.server.auth.model.BasicHttpToken;
import io.netty.channel.Channel;
public class AuthHttpProxyServer {
// curl -i -x 127.0.0.1:9999 -U admin:123456 https://www.baidu.com
public static void main(String[] args) throws Exception {
HttpProxyServerConfig config = new HttpProxyServerConfig();
config.setAuthenticationProvider(new BasicHttpProxyAuthenticationProvider() {
@Override
protected BasicHttpToken authenticate(String usr, String pwd) {
if ("admin".equals(usr) && "123456".equals(pwd)) {
return new BasicHttpToken(usr, pwd);
}
return null;
}
});
new HttpProxyServer()
.serverConfig(config)
.proxyInterceptInitializer(new HttpProxyInterceptInitializer() {
@Override
public void init(HttpProxyInterceptPipeline pipeline) {
pipeline.addLast(new HttpProxyIntercept() {
@Override
public void beforeConnect(Channel clientChannel, HttpProxyInterceptPipeline pipeline) throws Exception {
System.out.println(HttpAuthContext.getToken(clientChannel));
}
});
}
})
.start(9999);
}
}