美文网首页
nginx 邮件代理认证(imap pop smtp)

nginx 邮件代理认证(imap pop smtp)

作者: 贰爷 | 来源:发表于2020-07-24 17:13 被阅读0次

    nginx安装的时候要编译添加mail 支持 --with-mail --with-mail_ssl_module

    nginx配置文件

    worker_processes  4;
    error_log  logs/error.log  info;
    events {
        worker_connections  1024;
    }
    mail {
        auth_http  127.0.0.1:8070;
        pop3_capabilities  "TOP"  "USER";
        imap_capabilities  "IMAP4rev1"  "UIDPLUS";
        smtp_capabilities "SIZE 10485760" ENHANCEDSTATUSCODES 8BITMIME DSN;
    
        server {
                listen     110;
                protocol   pop3;
                proxy      on;
        }
        server {
                listen     143;
                protocol   imap;
                proxy      on;
        }
        server {
            listen    25;
            protocol    smtp;
            proxy    on;
            smtp_auth login plain;
            xclient    off;
        }
    }
    

    auth 认证127.0.0.1:8070 这边是采用的 tornado

    #!/usr/bin/env python
    #coding:utf-8
    
    import tornado.ioloop
    import tornado.web
    import tornado.httpserver 
    
    class IndexHandler(tornado.web.RequestHandler):
        def get(self):
            auth_user = self.request.headers['Auth-User']
            auth_pass = self.request.headers['Auth-Pass']
            client_ip = self.request.headers['Client-Ip']
            auth_protocol = self.request.headers['Auth-Protocol']
            self.set_header('Auth-Status','OK')
            self.set_header('Auth-Server','imap.mail.com')
            self.set_header('Auth-Port','143')
            
            return
    application = tornado.web.Application([
        (r"/", IndexHandler),
    
    ])      
    if __name__ == "__main__":
        application.listen(8070)
        tornado.ioloop.IOLoop.current().start()
    

    php版api认证

    <?php
    if(!isset($_SERVER ["HTTP_AUTH_USER"] ) || ! isset($_SERVER ["HTTP_AUTH_PASS"] )) {
        header("Auth-Status: Invalid login or password");
        exit;
    }
    
    $auth_user = $_SERVER['HTTP_AUTH_USER'];
    $auth_pass = $_SERVER['HTTP_AUTH_PASS'];
    $auth_protocol = $_SERVER['HTTP_AUTH_PROTOCOL'];
    list($uid, $domain) = explode("@", $auth_user);
    
    
    $conn = mysql_connect("127.0.0.1","root","123456");
    if (!$conn){
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("mail", $conn);
    
    $result = mysql_query("select host from mail where pdomain='".$domain."' limit 1");
    while($row=mysql_fetch_array($result)){
        $host = $row['host'];   
    }
    /* PHP7使用mysqli连接数据库
    
    $con = new mysqli($dbhost,$dbuser,$dbpwd,$dbname);
    
    if(mysqli_connect_errno()){
        echo "mysqli connect fail: " .mysqli_connect_error();
        exit();
    }
    
    $result = $con->query("select host from mail where pdomain='".$domain."' limit 1");
    while($row=mysqli_fetch_array($result)){
        $host = $row['host'];   
    }
    */
    switch($host){
        case "mail1.com":
            $backend = "192.168.1.1";
            break;
        case "mail2.com":
            $backend = "192.168.1.2";
            break;
    }
    
    if ($auth_protocol == "imap"){
        //header("Auth-Status:OK");
        header("Auth-Server:$backend");
        header("Auth-Port:143");
        exit();
    }
    else if($auth_protocol == "pop3"){
        //header("Auth-Status:OK");
        header("Auth-Server:$backend");
        header("Auth-Port:110");
        exit();
    }
    else if($auth_protocol == "smtp"){
        #header("Auth-Status:OK");
        header("Auth-Server:$backend");
        header("Auth-Port:25");
        exit();
    }
    
    ?>
    

    相关文章

      网友评论

          本文标题:nginx 邮件代理认证(imap pop smtp)

          本文链接:https://www.haomeiwen.com/subject/ulhslktx.html