Proxy server (squid)

.·2023년 11월 20일
0

squid-proxy-setting

환경

  • ubuntu 22.04
  1. apt-get update 및 패캐지 다운로드
sudo apt-get update
sudo apt-get install squid

# squid 인증을 위해 생성
sudo apt-get install apache2-utils

1.5. 미리 squid 테스트

sudo systemctl start squid
sudo systemctl enable squid
  1. SSL 사설 인증서 생성
sudo chmod 777 /etc/squid

cd /etc/squid
mkdir ssl
cd ssl

# pem key 는 아무렇게 설정해도 상관없음
openssl genrsa -des3 -out squid.key 1024 
openssl req -new -key squid.key -out squid.csr
cp squid.key squid.key.org
openssl rsa -in squid.key.org -out squid.key
openssl x509 -req -days 365 -in squid.csr -signkey squid.key -out squid.crt
  1. 인증 사용자를 위한 사용자명과 비밀번호 생성 및 squid 설정 파일 수정
sudo htpasswd -c /etc/squid/passwd myuser

sudo vi /etc/squid/squid.conf
# 아래 설정파일에 인증 코드가 있지만 설명을 위해 작성.
# 설명 : squid 인증을 위한 코드. 만약 인증이 필요없다면 삭제 또는 주석처리 하면 됨.
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
  1. squid 설정파일 생성
sudo vi /etc/squid/squid.conf

#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
# IP를 한정할 수 없어서 전체 오픈. 필요시 지정 가능.
# acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl allnetwork src all

# 허용 포트 지정. HTTPS만 사용.
acl SSL_ports port 443
acl Safe_ports port 443         # https

acl CONNECT method CONNECT

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

http_access allow localhost manager
http_access deny manager

# htpasswd 인증 설정 추가
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated

# 허용 도메인 추가.
acl allow_dst dstdomain .xxx.com
http_access allow allow_dst

# http_access allow localnet
# http_access allow localhost
http_access allow allnetwork

http_access deny all

# 해킹 방지를 위해서 기존 오류 페이지 변경을 위해서 페이지 위치 변경
# error_directory /etc/squid/page

# Squid normally listens to port 3128
# HTTPS만 사용할거라서 인증서 위치 지정. 기본 포트 변경하는것이 좋음
# http_port 13128 # 만약 http 도 사용할 거면 주석 해제
https_port 13128 tls-cert=/etc/squid/ssl/squid.crt tls-key=/etc/squid/ssl/squid.key

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256
# 캐쉬는 사용하지 않음
cache deny all

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid
  1. squid 실행
sudo systemctl start squid
sudo systemctl enable squid

# OR
sudo systemctl restart squid
  1. prxoy 클라이언트 구현
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


proxies = {
  'https':'https://<user id>:<password>@<x.x.x.x IP>:<port no>'
}

url = "https://api.upbit.com/v1/market/all?isDetails=false"

resp = requests.get(url, proxies=proxies, verify=False)

print(resp.json())

reference

etc

  • 설정파일이 오류가 있으면 systemctl start squid 할 때 에러나는 경우가 많음. 'journalctl -xeu squid.service' 을 통해 에러 로그를 확인하면서 해결하면 간단함.
profile
.

0개의 댓글