Ssh設(shè)置代理

背景

有些主機(jī)有些端口有訪問限制,
比如只有辦公室網(wǎng)絡(luò)可以訪問,
比如有兩個(gè)集群,192.168.107.* 192.168.106.*,
107的服務(wù),限制ip訪問,106集群不能訪問。

socks5代理:

ssh -gR 1111:localhost:22 n1
本地服務(wù)映射到遠(yuǎn)程

ssh -gL 2222:localhost:32567 n1
遠(yuǎn)程服務(wù),通過本地端口訪問

ssh -p 1111 -gD 4444 yiping@localhost
遠(yuǎn)程4444端口作為n1代理訪問

http(s)代理:

本機(jī)1080端口啟動(dòng)http

# encoding:utf-8
import socket
import _thread


class Header:
   """
   用于讀取和解析頭信息
   """

   def __init__(self, conn):
       self._method = None
       header = b''
       try:
           while 1:
               data = conn.recv(4096)
               header = b"%s%s" % (header, data)
               if header.endswith(b'\r\n\r\n') or (not data):
                   break
       except:
           pass
       self._header = header
       self.header_list = header.split(b'\r\n')
       self._host = None
       self._port = None

   def get_method(self):
       """
       獲取請(qǐng)求方式
       :return:
       """
       if self._method is None:
           self._method = self._header[:self._header.index(b' ')]
       return self._method

   def get_host_info(self):
       """
       獲取目標(biāo)主機(jī)的ip和端口
       :return:
       """
       if self._host is None:
           method = self.get_method()
           line = self.header_list[0].decode('utf8')
           if method == b"CONNECT":
               host = line.split(' ')[1]
               if ':' in host:
                   host, port = host.split(':')
               else:
                   port = 443
           else:
               for i in self.header_list:
                   if i.startswith(b"Host:"):
                       host = i.split(b" ")
                       if len(host) < 2:
                           continue
                       host = host[1].decode('utf8')
                       break
               else:
                   host = line.split('/')[2]
               if ':' in host:
                   host, port = host.split(':')
               else:
                   port = 80
           self._host = host
           self._port = int(port)
       return self._host, self._port

   @property
   def data(self):
       """
       返回頭部數(shù)據(jù)
       :return:
       """
       return self._header

   def is_ssl(self):
       """
       判斷是否為 https協(xié)議
       :return:
       """
       if self.get_method() == b'CONNECT':
           return True
       return False

   def __repr__(self):
       return str(self._header.decode("utf8"))


def communicate(sock1, sock2):
   """
   socket之間的數(shù)據(jù)交換
   :param sock1:
   :param sock2:
   :return:
   """
   try:
       while 1:
           data = sock1.recv(1024)
           if not data:
               return
           sock2.sendall(data)
   except:
       pass


def handle(client):
   """
   處理連接進(jìn)來的客戶端
   :param client:
   :return:
   """
   timeout = 60
   client.settimeout(timeout)
   header = Header(client)
   if not header.data:
       client.close()
       return
   print(*header.get_host_info(), header.get_method())
   server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   try:
       server.connect(header.get_host_info())
       server.settimeout(timeout)
       if header.is_ssl():
           data = b"HTTP/1.0 200 Connection Established\r\n\r\n"
           client.sendall(data)
           _thread.start_new_thread(communicate, (client, server))
       else:
           server.sendall(header.data)
       communicate(server, client)
   except:
       server.close()
       client.close()


def serve(ip, port):
   """
   代理服務(wù)
   :param ip:
   :param port:
   :return:
   """
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   s.bind((ip, port))
   s.listen(10)
   print('proxy start...')
   while True:
       conn, addr = s.accept()
       _thread.start_new_thread(handle, (conn,))


if __name__ == '__main__':
   IP = "0.0.0.0"
   PORT = 1080
   serve(IP, PORT)

本機(jī)ssh映射到遠(yuǎn)程
ssh -gR 1111:localhost:22 n1

遠(yuǎn)程映射轉(zhuǎn)發(fā)代理到本機(jī)
ssh -p 1111 -gL 4444:localhost:1080 yiping@localhost

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容