Browse Source

response 增加长链接 ,请求增加User-Agent

zhangnaiwen 2 years ago
parent
commit
a1f0f5d88a
4 changed files with 59 additions and 34 deletions
  1. 2 2
      conf/uwsgi.ini
  2. 10 1
      docker-compose.yml
  3. 38 22
      src/app/api/apis.py
  4. 9 9
      src/app/configs/config.py

+ 2 - 2
conf/uwsgi.ini

@@ -29,7 +29,7 @@ master = true
 #async=128
 
 # 进程数
-processes = 2
+processes = 10
 if-env = UWSGI_PROCESSES
 processes = $(UWSGI_PROCESSES)
 endif =
@@ -53,7 +53,7 @@ http-keepalive = $(UWSGI_HTTP_KEEPALIVE)
 endif =
 
 # 超时关闭连接的时间(秒)
-harakiri = 20
+harakiri = 200
 if-env = UWSGI_HARAKIRI
 harakiri = $(UWSGI_HARAKIRI)
 endif =

+ 10 - 1
docker-compose.yml

@@ -13,9 +13,18 @@ service:
     - UWSGI_HTTP_KEEPALIVE=false
     - UWSGI_HARAKIRI=20
     - UWSGI_PY_OPTIMIZE=0
+    # 代理服务地址
+    - NatGeo_World_Map=https://server.arcgisonline.com/arcgis/rest/services/NatGeo_World_Map/MapServer
+    - USA_Topo_Maps=https://server.arcgisonline.com/arcgis/rest/services/USA_Topo_Maps/MapServer
+    - World_Imagery=https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer
+    - World_Physical_Map=https://server.arcgisonline.com/arcgis/rest/services/World_Physical_Map/MapServer
+    - World_Shaded_Relief=https://server.arcgisonline.com/arcgis/rest/services/World_Shaded_Relief/MapServer
+    - World_Street_Map=https://server.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer
+    - World_Terrain_Base=https://server.arcgisonline.com/arcgis/rest/services/World_Terrain_Base/MapServer
+    - World_Topo_Map=https://server.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer
+    - 3DModel=http://192.168.1.1:80/3DTiles/qp/QPXZFWZX/JJ
 
   volumes:
-    - D:\code\proxy_nginx_server\src\config.py:/work/nginx_agent/src/configs/config.py
     - D:\data/logs:/data/logs
 
   privileged: true

+ 38 - 22
src/app/api/apis.py

@@ -8,21 +8,31 @@ from app.configs import config
 
 
 def verify_token(token, servertype):
-    if token not in config.built_in_token or token not in config.token_dict:  # 不是内置token请求oauth或不在字记录典里请求oauth
-        # token ,记录token,token为key,时间错为value
-        validate_token_rep = requests.post('http://%s/oauth/api/user/getUserByToken' % config.OAUTH_HOST,
-                                           headers={'token': token},
-                                           data={'strUrl': servertype, 'serviceId': 8})
-
-    elif time.time() > config.token_dict.get(token) + 172800:
-        # token在字典里,判断token是否超过48小时,超过删除token请求oauth。记录token,token为key,时间错为value
-        config.token_dict.pop(token)
-        validate_token_rep = requests.post('http://%s/oauth/api/user/getUserByToken' % config.OAUTH_HOST,
-                                           headers={'token': token},
-                                           data={'strUrl': servertype, 'serviceId': 8}
-                                           )
-
-    return validate_token_rep.json()
+    if token not in config.built_in_token:  # 不是内置token请求oauth
+        # token 不在字典里请求oauth,记录token,token为key,时间错为value
+        if token not in config.token_dict:
+            validate_token_rep = requests.post('http://%s/oauth/api/user/getUserByToken' % config.OAUTH_HOST,
+                                               headers={'token': token,
+                                                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'},
+                                               data={'strUrl': servertype, 'serviceId': 8}).json()
+            if validate_token_rep:
+                if validate_token_rep.get('code') != 200:
+                    return validate_token_rep
+            return validate_token_rep
+
+        else:
+            # token在字典里,判断token是否超过48小时,超过删除token请求oauth。记录token,token为key,时间错为value
+            if time.time() > config.token_dict.get(token) + 172800:
+                config.token_dict.pop(token)
+                validate_token_rep = requests.post('http://%s/oauth/api/user/getUserByToken' % config.OAUTH_HOST,
+                                                   headers={'token': token,
+                                                            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'},
+                                                   data={'strUrl': servertype, 'serviceId': 8}
+                                                   ).json()
+                if validate_token_rep:
+                    if validate_token_rep.get('code') != 200:
+                        return validate_token_rep
+                return validate_token_rep
 
 
 class AgentApi(Resource):
@@ -35,15 +45,17 @@ class AgentApi(Resource):
             servertype = args.pop('servertype')
 
             validate_token_rep = verify_token(token, servertype)
-            if validate_token_rep.get('code') != 200:
-                return validate_token_rep
+            if validate_token_rep:
+                if validate_token_rep.get('code') != 200:
+                    return validate_token_rep
 
-            config.token_dict[token] = time.time()
             # 替换ip转发请求
             rep = requests.get('{server}/{path}'.format(server=config.ServerConfig.get(servertype), path=path),
-                               params=args)
+                               params=args, headers={
+                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'})
             response = make_response(rep.content)
             response.headers['Content-Type'] = rep.headers.get('Content-Type')
+            response.headers['Connection'] = 'keep-alive'
 
             return response
 
@@ -59,13 +71,17 @@ class MapServer(Resource):
             servertype = args.pop('servertype')
 
             validate_token_rep = verify_token(token, servertype)
-            if validate_token_rep.get('code') != 200:
-                return validate_token_rep
+            if validate_token_rep is not None:
+                if validate_token_rep.get('code') != 200:
+                    return validate_token_rep
             # 替换ip转发请求
-            rep = requests.get(config.ServerConfig.get(servertype), params=args)
+            rep = requests.get(config.ServerConfig.get(servertype), params=args, headers={
+                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'})
 
             response = make_response(rep.text)
             response.headers['Content-Type'] = 'application/javascript;charset=UTF-8'
+            response.headers['Connection'] = 'keep-alive'
+
             return response
 
         except KeyError:

+ 9 - 9
src/app/configs/config.py

@@ -22,21 +22,21 @@ built_in_token = [
 
 # 代理服务配置
 ServerConfig = {
-    'NatGeo_World_Map': 'https://server.arcgisonline.com/arcgis/rest/services/NatGeo_World_Map/MapServer',
+    'NatGeo_World_Map': os.environ.get('NatGeo_World_Map', 'https://server.arcgisonline.com/arcgis/rest/services/NatGeo_World_Map/MapServer'),
 
-    'USA_Topo_Maps': 'https://server.arcgisonline.com/arcgis/rest/services/USA_Topo_Maps/MapServer',
+    'USA_Topo_Maps': os.environ.get('USA_Topo_Maps','https://server.arcgisonline.com/arcgis/rest/services/USA_Topo_Maps/MapServer'),
 
-    'World_Imagery': 'https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer',
+    'World_Imagery': os.environ.get('World_Imagery', 'https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer'),
 
-    'World_Physical_Map': 'https://server.arcgisonline.com/arcgis/rest/services/World_Physical_Map/MapServer',
+    'World_Physical_Map': os.environ.get('World_Physical_Map'),
 
-    'World_Shaded_Relief': 'https://server.arcgisonline.com/arcgis/rest/services/World_Shaded_Relief/MapServer',
+    'World_Shaded_Relief': os.environ.get('World_Shaded_Relief'),
 
-    'World_Street_Map': 'https://server.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer',
+    'World_Street_Map': os.environ.get('World_Street_Map'),
 
-    'World_Terrain_Base': 'https://server.arcgisonline.com/arcgis/rest/services/World_Terrain_Base/MapServer',
+    'World_Terrain_Base': os.environ.get('World_Terrain_Base'),
 
-    'World_Topo_Map': 'https://server.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer',
+    'World_Topo_Map': os.environ.get('World_Topo_Map'),
 
-    '3DModel': 'http://192.168.1.37:80/model_data/QPXZFUZX/20220922-1/output'
+    '3DModel': os.environ.get('3DModel', 'http://122.228.13.28:1009/3DTiles/qp/QPXZFWZX/JJ/android/1C/')
 }