Proxy Server源码及分析

本文详细解析了代理服务器(Proxy Server)的工作原理,通过分析Carl Harris编写的源码,阐述了代理服务器如何作为转发中介,实现客户端与远程服务器的通信。核心函数do_proxy展示了代理服务器如何创建新套接字并建立与远程服务器的连接,同时利用select函数处理来自客户端和服务器的数据。文章还介绍了守护进程的创建过程,确保程序能在后台稳定运行。
摘要由CSDN通过智能技术生成

本文主要讲代理服务器源码,是一位叫Carl Harris大神写的,非常简单易懂,把代理服务器(Proxy Server)本质完全体现出来。相信读懂了这段代码,以后想搞定http代理等其他类型的代理服务器也能行。在附录中会贴出proxy全部源码,仅供学习使用。

一、代理服务器的定义

代理服务器(Proxy Server)是一种重要的服务器安全功能,它的工作主要在开放系统互联(OSI)模型的会话层,从而起到防火墙的作用。代理(英语:Proxy),也称网络代理,是一种特殊的网络服务,允许一个网络终端(一般为客户端)通过这个服务与另一个网络终端(一般为服务器)进行非直接的连接。一些网关、路由器等网络设备具备网络代理功能。一般认为代理服务有利于保障网络终端的隐私或安全,防止攻击。
以上截取了网上Proxy Server的定义。看起来晦涩难懂。简单来说,代理服务器就是起到一个转发功能。比如,你在本机A想要访问国外的服务器C,你本机没权限访问C,需要通过向服务器B发送数据,B再把你的数据发送给C,C返回数据也是先把数据交给了B,然后B再转交给你。这里B服务器别名为代理服务器(Proxy Server)。等会分析到proxy源码,就更加清楚了。

二、proxy源码分析

以下是proxy的主程序。

int main(int argc, char **argv)
{
    int clilen;
    int childpid;
    int sockfd, newsockfd;
    struct sockaddr_in servaddr, cliaddr;
    parse_args(argc, argv);//prepare an address struct to listen for connect
    bzero((char*)&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = proxy_port;
    //get asocket ..
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
      fputs("failed to crate server socket\r\n", stderr);
      exit(1);
    }
    // and bind our address and port to it
    if(bind(sockfd, (struct sockaddr_in*)&servaddr, sizeof(servaddr)) < 0)
    {
      fputs("failed to bind server socket to specified/r/n", stderr);
      exit(1);
    }
    // get ready to accept with at most 5 clients waitting to connect
    listen(sockfd, 5);
    // turn ourselves into daemon
    daemonize(sockfd);
    //fall into a loop to accept new connections and spawn children
    while(1)
    {
        //accept the next connection
        clilen = sizeof(cliaddr);
        newsockfd = accept(sockfd, (struct sockaddr*)&cliaddr, &clilen);
        if(newsockfd <0 && errno == EINTR)
          continue;
        //a signal might interrupt our accept call
        else if(newsockfd < 0)
          errorout("failed to accept connection");//sth quiet amiss--kill server
        //fork a child to handle this connection
        if((childpid = fork()) == 0)
        {
            close(sockfd);// inherit
            do_proxy(newsockfd);
            exit(0);
        }
        // if fork falied the connection is silently dropped --oops
        close(newsockfd);
    }
    return 0;
}

从上面的程序看出,先是用函数parse_args(argc, argv) 解析获得用户输入的信息,包括想要绑定的代理服务器端口号、远程主机的host name 和 远程主机的服务号。获取之后设置到全局变量。接着用socket函数创建tcp 套接字,用bind函数绑定刚才获取到的端口号,用listen监听有没有客户端连接到代理服务器。然后,程序调用daemonize(sockfd)函数,这个函数的作用是创建一个守护进程。程序往下是在while循环里面调用accept函数阻塞,如果发现有客户端连接,就返回一个套接字,并fork出子进程处理这个套接字发过来的请求。do_proxy函数就是专门来干这事的。
do_proxy函数代码如下:

void do_proxy (int usersockfd)
{
    int isosockfd;
    fd_set rdfdset;
    int connstat;
    int iolen;
    char buf[2048];
    /* open a socket to connect to the isolated host */
    if ((isosockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
      errorout("failed to create socket to host");
    /* attempt a connection */
    connstat = connect(isosockfd,(struct sockaddr *) &hostaddr, sizeof(hostaddr));
    switch (connstat)
    {
        
Nacos 注册服务源码涉及的主要类有以下几个: 1. NamingService:命名服务接口,定义了注册、注销、查询服务的方法。 2. NamingServiceImpl:命名服务的实现类,实现了 NamingService 接口。 3. ServerListManager:服务列表管理器,负责管理服务列表。 4. Instance:服务实例对象,包含了服务实例的基本信息。 5. Service:服务对象,包含了服务的基本信息和服务实例列表。 6. ServiceInfo:服务信息对象,包含了所有服务和服务实例的信息。 7. ServerProxy:服务代理类,负责与服务注册中心通信。 下面对这些类进行详细的分析。 1. NamingService 命名服务接口,定义了注册、注销、查询服务的方法。 ```java public interface NamingService { /** * 注册一个服务实例 */ void registerInstance(String serviceName, Instance instance) throws NacosException; /** * 注销一个服务实例 */ void deregisterInstance(String serviceName, Instance instance) throws NacosException; /** * 查询一个服务的所有实例 */ List<Instance> getAllInstances(String serviceName) throws NacosException; /** * 监听一个服务的实例变化 */ void subscribe(String serviceName, EventListener listener) throws NacosException; /** * 取消监听一个服务的实例变化 */ void unsubscribe(String serviceName, EventListener listener) throws NacosException; } ``` 2. NamingServiceImpl 命名服务的实现类,实现了 NamingService 接口。 ```java public class NamingServiceImpl implements NamingService { private ServerProxy serverProxy; public NamingServiceImpl(ServerProxy serverProxy) { this.serverProxy = serverProxy; } @Override public void registerInstance(String serviceName, Instance instance) throws NacosException { serverProxy.registerService(serviceName, instance); } @Override public void deregisterInstance(String serviceName, Instance instance) throws NacosException { serverProxy.deregisterService(serviceName, instance); } @Override public List<Instance> getAllInstances(String serviceName) throws NacosException { return serverProxy.getAllInstances(serviceName); } @Override public void subscribe(String serviceName, EventListener listener) throws NacosException { serverProxy.subscribe(serviceName, listener); } @Override public void unsubscribe(String serviceName, EventListener listener) throws NacosException { serverProxy.unsubscribe(serviceName, listener); } } ``` 3. ServerListManager 服务列表管理器,负责管理服务列表。 ```java public class ServerListManager { private DiscoveryConfig config; private List<ServerInfo> serverList = new ArrayList<>(); private AtomicInteger index = new AtomicInteger(0); public ServerListManager(DiscoveryConfig config) { this.config = config; initServerList(); } private void initServerList() { // 读取配置文件中的服务地址列表 String serverListStr = config.getServerList(); String[] serverArr = serverListStr.split(","); for (int i = 0; i < serverArr.length; i++) { String[] serverInfoArr = serverArr[i].split(":"); String ip = serverInfoArr[0]; int port = Integer.parseInt(serverInfoArr[1]); ServerInfo serverInfo = new ServerInfo(ip, port); serverList.add(serverInfo); } } public ServerInfo getNextServer() { // 轮询获取服务地址 int i = index.getAndIncrement() % serverList.size(); return serverList.get(i); } } ``` 4. Instance 服务实例对象,包含了服务实例的基本信息。 ```java public class Instance { private String serviceName; private String ip; private int port; public Instance(String serviceName, String ip, int port) { this.serviceName = serviceName; this.ip = ip; this.port = port; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } } ``` 5. Service 服务对象,包含了服务的基本信息和服务实例列表。 ```java public class Service { private String name; private List<Instance> instances = new ArrayList<>(); public Service(String name) { this.name = name; } public String getName() { return name; } public List<Instance> getInstances() { return instances; } public void addInstance(Instance instance) { instances.add(instance); } public void removeInstance(Instance instance) { instances.remove(instance); } } ``` 6. ServiceInfo 服务信息对象,包含了所有服务和服务实例的信息。 ```java public class ServiceInfo { private Map<String, Service> serviceMap = new HashMap<>(); public void addService(Service service) { serviceMap.put(service.getName(), service); } public void removeService(Service service) { serviceMap.remove(service.getName()); } public List<Service> getServices() { return new ArrayList<>(serviceMap.values()); } } ``` 7. ServerProxy 服务代理类,负责与服务注册中心通信。 ```java public class ServerProxy { private ServerListManager serverListManager; public ServerProxy(ServerListManager serverListManager) { this.serverListManager = serverListManager; } public void registerService(String serviceName, Instance instance) throws NacosException { ServerInfo serverInfo = serverListManager.getNextServer(); try (Socket socket = new Socket(serverInfo.getIp(), serverInfo.getPort())) { // 向注册中心发送注册请求 // ... } catch (IOException e) { // 处理异常 } } public void deregisterService(String serviceName, Instance instance) throws NacosException { // 向注册中心发送注销请求 // ... } public List<Instance> getAllInstances(String serviceName) throws NacosException { // 从注册中心获取服务实例列表 // ... return instances; } public void subscribe(String serviceName, EventListener listener) throws NacosException { // 向注册中心发送订阅请求 // ... } public void unsubscribe(String serviceName, EventListener listener) throws NacosException { // 向注册中心发送取消订阅请求 // ... } } ``` 以上就是 Nacos 注册服务源码分析的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值