Skip to content

Instantly share code, notes, and snippets.

@outiy5
Last active May 17, 2022 09:45
Show Gist options
  • Save outiy5/57cb1a824a0c6d2dd527fb7f3e6be121 to your computer and use it in GitHub Desktop.
Save outiy5/57cb1a824a0c6d2dd527fb7f3e6be121 to your computer and use it in GitHub Desktop.

Revisions

  1. outiy5 revised this gist May 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,6 @@ sudo python reverse_proxy.py

    https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt-servers-certificate-chain-is-incomplete twisted letsencrypt.
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy.
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https.
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https. twisted ssl.
    https://twistedmatrix.com/documents/13.1.0/core/examples/index.html examples.
    https://stackoverflow.com/questions/35664007/python-twisted-reverse-proxy-to-https-api-could-not-connect proxy to https url.
  2. outiy5 revised this gist May 17, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -50,3 +50,4 @@ https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy.
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https.
    https://twistedmatrix.com/documents/13.1.0/core/examples/index.html examples.
    https://stackoverflow.com/questions/35664007/python-twisted-reverse-proxy-to-https-api-could-not-connect proxy to https url.
  3. outiy5 revised this gist May 17, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,10 @@ reactor.listenSSL(443, site, contextFactory)
    reactor.run()
    ```

    ## HTTPS to HTTPS

    Just implement one more factory for `proxy.ReverseProxyResource("127.0.0.1", 8000, b"")` to apply to https connections. See references for examples. I would update this part later.

    ## HTTP to HTTP

    ```
  4. outiy5 revised this gist May 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ from twisted.web import proxy, server
    log.startLogging(sys.stdout)
    from OpenSSL import crypto
    privkey=open('privkey.pem', 'rt').read() # copy letsencrypt cerficate here
    privkey=open('privkey.pem', 'rt').read() # copy letsencrypt certficates here
    certif=open('cert.pem', 'rt').read()
    chain=open('chain.pem', 'rt').read()
    privkeypyssl=crypto.load_privatekey(crypto.FILETYPE_PEM,privkey)
  5. outiy5 revised this gist May 17, 2022. No changes.
  6. outiy5 revised this gist May 17, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ sudo python reverse_proxy.py

    ## Reference

    https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt-servers-certificate-chain-is-incomplete twisted letsencrypt.
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy
    https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt-servers-certificate-chain-is-incomplete twisted letsencrypt.
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy.
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https.
    https://twistedmatrix.com/documents/13.1.0/core/examples/index.html examples.
  7. outiy5 revised this gist May 17, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ sudo python reverse_proxy.py

    ## Reference

    https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt-servers-certificate-chain-is-incomplete twisted letsencrypt
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https
    https://twistedmatrix.com/documents/13.1.0/core/examples/index.html examples
    https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt-servers-certificate-chain-is-incomplete twisted letsencrypt.
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https.
    https://twistedmatrix.com/documents/13.1.0/core/examples/index.html examples.
  8. outiy5 revised this gist May 17, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,10 @@ reactor.run()

    ## Dependencies

    ```
    sudo pip install service_identity twisted
    sudo python reverse_proxy.py
    ```

    ## Reference

  9. outiy5 created this gist May 17, 2022.
    46 changes: 46 additions & 0 deletions reverseproxy.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    ## HTTPS to HTTP

    `reverse_proxy.py`:
    ```
    import sys
    from twisted.internet import ssl, protocol, task, defer
    from twisted.python import log
    from twisted.internet import reactor
    from twisted.web import proxy, server
    log.startLogging(sys.stdout)
    from OpenSSL import crypto
    privkey=open('privkey.pem', 'rt').read() # copy letsencrypt cerficate here
    certif=open('cert.pem', 'rt').read()
    chain=open('chain.pem', 'rt').read()
    privkeypyssl=crypto.load_privatekey(crypto.FILETYPE_PEM,privkey)
    certifpyssl=crypto.load_certificate(crypto.FILETYPE_PEM,certif)
    chainpyssl=[crypto.load_certificate(crypto.FILETYPE_PEM,chain)]
    contextFactory=ssl.CertificateOptions(privateKey=privkeypyssl,certificate=certifpyssl,extraCertChain=chainpyssl)
    site = server.Site(proxy.ReverseProxyResource("127.0.0.1", 8000, b""))
    reactor.listenSSL(443, site, contextFactory)
    reactor.run()
    ```

    ## HTTP to HTTP

    ```
    from twisted.internet import reactor
    from twisted.web import proxy, server
    site = server.Site(proxy.ReverseProxyResource("example.com", 80, b""))
    reactor.listenTCP(8080, site)
    reactor.run()
    ```

    ## Dependencies

    sudo pip install service_identity twisted
    sudo python reverse_proxy.py

    ## Reference

    https://stackoverflow.com/questions/38766244/pyopenssl-twisted-with-lets-encrypt-servers-certificate-chain-is-incomplete twisted letsencrypt
    https://twistedmatrix.com/documents/current/web/examples/ reverse proxy
    https://twistedmatrix.com/documents/current/core/howto/ssl.html https
    https://twistedmatrix.com/documents/13.1.0/core/examples/index.html examples