Skip to content

Commit

Permalink
创建frp插件
Browse files Browse the repository at this point in the history
  • Loading branch information
zfb132 committed Jul 8, 2020
1 parent c1116fb commit 045b6d7
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frp插件开发
本代码运行在特定端口用于监听frp的RPC消息
## 运行代码
首先在当前项目主目录下创建虚拟环境
`python3 -m venv venv`
然后安装`requirements.txt`文件的依赖
`pip install -r requirements.txt`
然后输入以下命令启动uwsgi:
`source venv/bin/activate && uwsgi --ini uwsgi_frp-info.ini -d /dev/null && deactivate`
再添加以下内容到文件`frps.ini`
```conf
[plugin.frp-info]
addr = 127.0.0.1:6666
path = /handler
ops = Login,NewProxy,NewWorkConn,NewUserConn
```
最后重启frps服务`service frps restart`即可实现插件的安装配置
12 changes: 12 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 20-07-08 19:25:56
from flask import Flask
import app.config as config

app = Flask(__name__)
app.secret_key = config.FLASK_SECRET_KEY

from app.controller.main import *

7 changes: 7 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 20-07-08 19:24:15

# flask秘钥
FLASK_SECRET_KEY = "qrd762^$#&*hgwd$%UTD(*#RYD"
Empty file added app/controller/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions app/controller/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 20-07-08 19:40:23
from app import app
from flask import request
import app.config as config
import logging
import json

from app.model.HandleFrpMsg import handlemsg

# 初始化
logging = logging.getLogger('runserver.main')

@app.route('/handler', methods=["POST", "GET"])
def handler():
if request.method == "GET":
logging.debug('GET访问')
data = [{'提示': "非法访问"}]
return json.dumps(data, ensure_ascii=False), 403
try:
logging.debug('POST访问')
data = request.json
# print(data)
# 处理来自frp的请求数据
handlemsg(data)
# 始终返回此信息:不拒绝连接,保持不变;即不对内容进行任何操作
response_data = {"reject": False, "unchange": True}
return json.dumps(response_data, ensure_ascii=False), 200
except Exception as e:
logging.error(repr(e))
return 404
19 changes: 19 additions & 0 deletions app/model/HandleFrpMsg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 2020-07-08 19:55

import logging

logging = logging.getLogger('runserver.handlefrpmsg')


# 处理frps的各种信息,包括以下几种
# Login、NewProxy、Ping、NewWorkConn、NewUserConn
def handlemsg(data):
operation = data['op']
# Ping操作每隔30s发送一次
if operation == 'Ping':
return
content = data['content']
logging.debug(content)
Empty file added app/model/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 20-07-08 19:23:11
import os
import logging
from logging.handlers import RotatingFileHandler

LOG_FORMAT = "%(asctime)s [%(funcName)s: %(filename)s,%(lineno)d] - %(levelname)s : %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S"
LOG_PATH = "./log/"

# 初始化日志文件配置
def initLog(fileName,logger):
# 创建日志文件夹
if not os.path.exists(LOG_PATH):
os.mkdir(LOG_PATH)
myapp = logging.getLogger(logger)
myapp.setLevel(logging.DEBUG)
# 切割日志文件
handler = RotatingFileHandler(LOG_PATH+fileName, maxBytes=128*1024, backupCount=60)
handler.setFormatter(logging.Formatter(LOG_FORMAT,DATE_FORMAT))
myapp.addHandler(handler)
return myapp
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
uwsgi
15 changes: 15 additions & 0 deletions restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /bin/bash
PORT=6666
kill -9 $(lsof -t -i:$PORT) > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "Kill $PORT port successfully!"
else
echo "Fail to kill $PORT port"
fi
NAME=~/frp-info/log/server.log
uwsgi uwsgi_frp-info.ini -d $NAME > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "Restart successfully!"
else
echo "Fail to restart"
fi
13 changes: 13 additions & 0 deletions runserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 20-07-08 19:20:22
from app import app
from log import initLog

logging = initLog('frp-info.log','runserver')

if __name__ == '__main__':
app.run(host="0.0.0.0", debug=False, port=6665)

application = app
24 changes: 24 additions & 0 deletions uwsgi_frp-info.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[uwsgi]
# http协议对客户端开发的端口号
# 端口保证与frps.ini的plugin的addr一致
http = 0.0.0.0:6666
# 应用目录,即python代码所在目录
pythonpath = ./
# web 应用python主程序
wsgi-file = ./runserver.py
# 一般在主运行程序里指定 app = Flask(__name__)
callable = app
# 工作进程数
processes = 4
# 线程数
threads = 2
# 指定日志文件
demonize = ./log/server.log
# 切割日志文件
log-maxsize = 1000000
# python 虚拟环境目录
home = ./venv
# 修改python代码后自动重启
python-autoreload = 1
# 方便重启
safe-pidfile = ./uwsgi-master.pid

0 comments on commit 045b6d7

Please sign in to comment.