Websockets使用Python中的标头进行保护

时间:2018-10-11 15:00:25

标签: python python-3.x security nginx websocket

对于我的应用程序,我正在使用安全的websocket,它工作正常。但我想再保证一点。

对于使用websockets库的websocket python服务器im(在asyncio上)。但是当我检查通过websockets.serve()发送的路径值时,我将始终获取套接字的路径,并且send_ip始终是本地的。

如何更改我的配置,以便可以阻止其他试图连接的ips

Server.py

import ssl
import asyncio
import logging
import websockets
import pathlib

logging.basicConfig()

STATE = {'value': 0}

USERS = set()

async def register(websocket):
    USERS.add(websocket)
    print("connection made!")

async def unregister(websocket):
    USERS.remove(websocket)

async def update(websocket):
    await websocket.send("Jobnumber: 1")

async def counter(websocket, path):
    await register(websocket)
    addr, seq = websocket.remote_address
    print(addr) #ALWAYS localhost
    print(path) #always the same path /server/sock (as configured in NGNIX)

    try:
        async for message in websocket:
            print (message)

    finally:
        await unregister(websocket)

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(
    pathlib.Path(__file__).with_name('privkey.pem'))

asyncio.get_event_loop().run_until_complete(
    websockets.serve(counter, '', 8004, ssl=ssl_context))
asyncio.get_event_loop().run_forever()

Nginx:

服务器{

root /var/www/html/;
index index.php index.html index.htm index.nginx-debian.html;

server_name [hidden];

location / {
    try_files $uri $uri/ =404;
}

location /server/sock {
    proxy_pass https://pythonserver;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

ssl_certificate /etc/letsencrypt/live/../fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/.../privkey.pem; # managed by

}

上游pythonserver {         服务器localhost:8004;

}

1 个答案:

答案 0 :(得分:0)

尝试将其用于Nginx,因为您也在使用websocket:

server {
        listen 80 ;
        server_name <url>;
        large_client_header_buffers 8 32k;
  if ($http_user_agent ~* Googlebot) {  
    return 403; 
  }
        access_log /var/log/nginx/access.log;
        location / {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep-Alive,Content-Type';
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass https://<url>:443;
                proxy_read_timeout 90;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_buffers 8 32k;
                proxy_buffer_size 64k;
        }
}




server {
      listen 443;
  server_name abc.com;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log warn;
        ssl on;
        ssl_certificate /etc/nginx/ssl/ssl.crt;
        ssl_certificate_key /etc/nginx/ssl/ssl.key;

       location / {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Keep-Alive';
                add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
                add_header 'Access-Control-Allow-Credentials' 'true';
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_next_upstream error;
                proxy_pass http://pythonserver;
                add_header X-Upstream  $upstream_addr;
                add_header Host $http_host;
                proxy_read_timeout 90;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_buffers 8 32k;
                proxy_buffer_size 64k;

        }

}

在2个地方更新url作为您的服务器名称。