frp icon indicating copy to clipboard operation
frp copied to clipboard

有没有限制连接的方法

Open s084088 opened this issue 2 years ago • 7 comments

Describe the feature request

请问下,有没有限制连接的方法,比如同一个IP只能建立1个连接

Describe alternatives you've considered

No response

Affected area

  • [ ] Docs
  • [ ] Installation
  • [X] Performance and Scalability
  • [ ] Security
  • [ ] User Experience
  • [ ] Test and Release
  • [ ] Developer Infrastructure
  • [ ] Client Plugin
  • [ ] Server Plugin
  • [ ] Extensions
  • [ ] Others

s084088 avatar Jan 26 '24 09:01 s084088

frp 本身没有。服务端插件 应该可以实现

xqzr avatar Jan 28 '24 15:01 xqzr

https://github.com/fatedier/frp/blob/dev/conf/frps_full_example.toml#L129 服务器端的config有个maxPortsPerClient不知道能否满足你的需求。

lli-debu avatar Feb 02 '24 05:02 lli-debu

frp 本身没有。服务端插件 应该可以实现

请问有参考样例或者文章吗?

s084088 avatar Feb 02 '24 05:02 s084088

https://github.com/fatedier/frp/blob/dev/conf/frps_full_example.toml#L129 服务器端的config有个maxPortsPerClient不知道能否满足你的需求。

多谢,但是这个看起来像是限制每个frpc的最大连接数量,我希望的是单个IP只能对某个frpc指向的服务创建一个连接,按理讲应该在frpc上配置

s084088 avatar Feb 02 '24 05:02 s084088

I wrote a code for your requiement. Just try it.

#!/usr/bin/env python3
import re
from collections import defaultdict
from datetime import datetime
import subprocess

# 日志文件路径
log_file_path = '/home/user/right.log'

# 输出文件路径
ban_ip_file = '/home/user/ban_ip.txt'
establishment_ip_file = '/home/user/establishment_ip.txt'

# IP状态计数
ip_counts = defaultdict(int)

# 成功连接的IP集合
established_ips = set()

def parse_log_file():
    with open(log_file_path, 'r') as file:
        next(file)  # Skip header
        for line in file:
            parts = line.strip().split()
            if len(parts) < 6:
                continue
            status = parts[1]  # Correct column for status
            # Correct extraction of IP address without port
            remote_ip = re.sub(r'\[::ffff:(.*?)\]', r'\1', parts[5])
            remote_ip = re.sub(r':\d+$', '', remote_ip)  # Remove port number
            
            if status == 'ESTAB':
                if remote_ip not in established_ips:
                    established_ips.add(remote_ip)
                    record_established_connection(remote_ip)
            elif status == 'TIME-WAIT':
                ip_counts[remote_ip] += 1

def record_established_connection(ip):
    with open(establishment_ip_file, 'a') as file:
        file.write(f"{datetime.now()}: Established connection from IP {ip}\n")
    print(f"Recorded established connection: {ip}")

def apply_iptables_rules():
    for ip, count in ip_counts.items():
        print('count',count)
        if count > 3:  # Threshold is 3
            subprocess.run(['sudo', 'iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'], stdout=subprocess.DEVNULL)
            with open(ban_ip_file, 'a') as file:
                file.write(f"{datetime.now()}: Blocked IP {ip} due to exceeding TIME-WAIT threshold\n")
            print(f"Blocked IP: {ip}")

if __name__ == '__main__':
    parse_log_file()
    apply_iptables_rules()

23 and 24 are Frps opened ports. Shell for Crontab task is given as follows:

ss -anp | grep ":23" >/home/user/right.log
sudo /home/user/monitor_and_block.py
ss -anp | grep ":24" >/home/user/right.log
sudo /home/user/monitor_and_block.py

fangzr avatar Feb 05 '24 15:02 fangzr

Issues go stale after 21d of inactivity. Stale issues rot after an additional 7d of inactivity and eventually close.

github-actions[bot] avatar Feb 27 '24 00:02 github-actions[bot]

I wrote a code for your requiement. Just try it.

#!/usr/bin/env python3
import re
from collections import defaultdict
from datetime import datetime
import subprocess

# 日志文件路径
log_file_path = '/home/user/right.log'

# 输出文件路径
ban_ip_file = '/home/user/ban_ip.txt'
establishment_ip_file = '/home/user/establishment_ip.txt'

# IP状态计数
ip_counts = defaultdict(int)

# 成功连接的IP集合
established_ips = set()

def parse_log_file():
    with open(log_file_path, 'r') as file:
        next(file)  # Skip header
        for line in file:
            parts = line.strip().split()
            if len(parts) < 6:
                continue
            status = parts[1]  # Correct column for status
            # Correct extraction of IP address without port
            remote_ip = re.sub(r'\[::ffff:(.*?)\]', r'\1', parts[5])
            remote_ip = re.sub(r':\d+$', '', remote_ip)  # Remove port number
            
            if status == 'ESTAB':
                if remote_ip not in established_ips:
                    established_ips.add(remote_ip)
                    record_established_connection(remote_ip)
            elif status == 'TIME-WAIT':
                ip_counts[remote_ip] += 1

def record_established_connection(ip):
    with open(establishment_ip_file, 'a') as file:
        file.write(f"{datetime.now()}: Established connection from IP {ip}\n")
    print(f"Recorded established connection: {ip}")

def apply_iptables_rules():
    for ip, count in ip_counts.items():
        print('count',count)
        if count > 3:  # Threshold is 3
            subprocess.run(['sudo', 'iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'], stdout=subprocess.DEVNULL)
            with open(ban_ip_file, 'a') as file:
                file.write(f"{datetime.now()}: Blocked IP {ip} due to exceeding TIME-WAIT threshold\n")
            print(f"Blocked IP: {ip}")

if __name__ == '__main__':
    parse_log_file()
    apply_iptables_rules()

23 and 24 are Frps opened ports. Shell for Crontab task is given as follows:

ss -anp | grep ":23" >/home/user/right.log
sudo /home/user/monitor_and_block.py
ss -anp | grep ":24" >/home/user/right.log
sudo /home/user/monitor_and_block.py

多谢,我找到一个其他的转发工具实现了

s084088 avatar Feb 27 '24 09:02 s084088