NGINX NJS(Javasciprt) 모듈 SNI/APLN Parsing(ssl_preread)

2022. 10. 27. 11:36·공부/Network

TLS 패킷 중 ClientHello의 SNI/ALPN 확인하여 라우팅 할 필요가 있다.

 

문제는 이게 들어오면 그냥 그대로 보내면 되는게 아니라,

해당 SNI에 따라 SSH Tunnel을 열고난 후, 거기로 포워딩을 하려 하기에 복잡해진다.

 

그래서 Nginx NJS 모듈을 사용해서 복잡도를 좀 낮춰보기위해서 시도해보았다.

 

TLS ClientHello 메시지를 파싱해서 SNI/ALPN등을 확인하고 그에 맞춰서 작업을 처리해주고,

그다음에 proxy_pass를 하는식으로 할 계획이다.

 

nginx.conf 다른거 다 쳐내고 중요한 내용만 남겼다.

# NJS 모듈 로드
load_module modules/ngx_stream_js_module.so;

stream {
    # JS file path
    js_path "/etc/nginx/njs/"; 
    # import stream.js and name it as "main"
    js_import main from stream.js;

    # ssl_preread doesn't work with NJS i think
    # ssl_preread on; 

    server {
            listen 443;

            # JS Preread
            js_preread main.hello;

            proxy_pass 127.0.0.1:22;
    }
}

 

JS 코드는 간단하게 이렇게 작성했다.

Client Hello 데이터 받으면 s.warn으로 로그 찍어주는것만 해놨다.

// stream.js

/// <reference path="../ts/ngx_stream_js_module.d.ts" />
/**
 * @param {NginxStreamRequest} s
 * */
function hello(s) {
    s.on('upload', function (data, flags) {
        s.warn('upload');        
        s.warn(data);

        // dbg purpose
        Object.keys(s).forEach(function (key) {
            s.warn(key + ': ' + s.variables[key]);
        });
    });
}

export default {hello}

 

ssl_preread on; 을 하면 난 아래 variable에 있는 ssl_preread_alpn_protocols, ssl_preread_server_name 이 설정 될 줄 알았는데, 인생은 그리 쉽지 않다.

 

문서를 보니 아래외 같이 적혀있다.

http://nginx.org/en/docs/njs/reference.html#stream
s.variables{}
nginx variables object, writable (since 0.2.8). A variable can be writable only if it is referenced in nginx configuration file. Even so, some embedded variables still cannot be assigned a value.

Writable only 인데, 이거에 쓰려면 conf에서 사용중이여야만 한다는 뜻.

 

아무래도 JS에서 Client Hello 파싱하고 여기다 write 한 후, conf에서 쓰는 것 같다.

 

예제를 봐도, 직접 파싱하고 처리하는 듯 허다.

function preread(s) {
    s.on('upload', function (data, flags) {
        var n = data.indexOf('\n');
        if (n != -1) {
            line = data.substr(0, n);
            s.done();
        }
    });
}

http://nginx.org/en/docs/stream/ngx_stream_js_module.html#properties

 

Module ngx_stream_js_module

Module ngx_stream_js_module The ngx_stream_js_module module is used to implement handlers in njs — a subset of the JavaScript language. Download and install instructions are available here. Example Configuration The example works since 0.4.0. stream { j

nginx.org

 

아무튼 이런 방법도 있는데, 있다는 것만 기억해두고

파이썬으로 돌아가서 작업할 듯 하다.

 

 

 

 

'공부 > Network' 카테고리의 다른 글

ARP(Address Resolution Protocol)  (0) 2022.06.18
'공부/Network' 카테고리의 다른 글
  • ARP(Address Resolution Protocol)
거북이황
거북이황
nomad.hwang@gmail.com
  • 거북이황
    거북이황 개발하는 이야기
    거북이황
  • 전체
    오늘
    어제
    • 분류 전체보기 (25)
      • 프로젝트 (4)
        • 댓글리 (4)
      • 개발 (19)
        • MCU (2)
        • Linux (3)
        • Project (1)
        • AWS (9)
      • 공부 (2)
        • Network (2)
        • CS (0)
  • 블로그 메뉴

    • Github
    • 홈
    • 태그
    • 방명록
  • 링크

    • Github
    • Gist
  • 공지사항

  • 인기 글

  • 태그

    nodejs 설치
    Fleet
    fleet provision
    IOT
    Ai
    aws
    iot provision
    Nodejs cmd
    SSH
    댓글리
    nodejs
    orangepi
    reverse tunneling
    fleet provisioning
    NodeJS LTS
    reverse proxy
    provisiong
    ssh proxy
    방화벽
    Nodejs 16
    Kernel
    Linux
    armbian
    오랜지파이
    빌드
    aws iot provision
    iot core
    ssh tunneling
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
거북이황
NGINX NJS(Javasciprt) 모듈 SNI/APLN Parsing(ssl_preread)
상단으로

티스토리툴바