Support Tor for LNbits recv (#1336)
* Add tor support to LNbits recv * Only return agent
This commit is contained in:
parent
d09f7c5427
commit
4cec369005
25
lib/cln.js
25
lib/cln.js
|
@ -1,30 +1,11 @@
|
|||
import fetch from 'cross-fetch'
|
||||
import https from 'https'
|
||||
import crypto from 'crypto'
|
||||
import { HttpProxyAgent, HttpsProxyAgent } from '@/lib/proxy'
|
||||
import { TOR_REGEXP } from '@/lib/url'
|
||||
import { getAgent } from '@/lib/proxy'
|
||||
|
||||
export const createInvoice = async ({ socket, rune, cert, label, description, msats, expiry }) => {
|
||||
let protocol, agent
|
||||
const httpsAgentOptions = { ca: cert ? Buffer.from(cert, 'base64') : undefined }
|
||||
const isOnion = TOR_REGEXP.test(socket)
|
||||
if (isOnion) {
|
||||
// we support HTTP and HTTPS over Tor
|
||||
protocol = cert ? 'https:' : 'http:'
|
||||
// we need to use our Tor proxy to resolve onion addresses
|
||||
const proxyOptions = { proxy: process.env.TOR_PROXY }
|
||||
agent = protocol === 'https:'
|
||||
? new HttpsProxyAgent({ ...proxyOptions, ...httpsAgentOptions, rejectUnauthorized: false })
|
||||
: new HttpProxyAgent(proxyOptions)
|
||||
} else if (process.env.NODE_ENV === 'development' && !cert) {
|
||||
protocol = 'http:'
|
||||
} else {
|
||||
// we only support HTTPS over clearnet
|
||||
agent = new https.Agent(httpsAgentOptions)
|
||||
protocol = 'https:'
|
||||
}
|
||||
const agent = getAgent({ hostname: socket, cert })
|
||||
|
||||
const url = `${protocol}//${socket}/v1/invoice`
|
||||
const url = `${agent.protocol}//${socket}/v1/invoice`
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
|
|
26
lib/proxy.js
26
lib/proxy.js
|
@ -1,5 +1,6 @@
|
|||
import http from 'http'
|
||||
import https from 'https'
|
||||
import { TOR_REGEXP } from '@/lib/url'
|
||||
|
||||
// from https://github.com/delvedor/hpagent
|
||||
|
||||
|
@ -118,3 +119,28 @@ export class HttpsProxyAgent extends https.Agent {
|
|||
request.end()
|
||||
}
|
||||
}
|
||||
|
||||
export function getAgent ({ hostname, cert }) {
|
||||
let agent
|
||||
|
||||
const httpsAgentOptions = { ca: cert ? Buffer.from(cert, 'base64') : undefined }
|
||||
|
||||
const isOnion = TOR_REGEXP.test(hostname)
|
||||
if (isOnion) {
|
||||
// we support HTTP and HTTPS over Tor
|
||||
const protocol = cert ? 'https:' : 'http:'
|
||||
// we need to use our Tor proxy to resolve onion addresses
|
||||
const proxyOptions = { proxy: process.env.TOR_PROXY }
|
||||
agent = protocol === 'https:'
|
||||
? new HttpsProxyAgent({ ...proxyOptions, ...httpsAgentOptions, rejectUnauthorized: false })
|
||||
: new HttpProxyAgent(proxyOptions)
|
||||
return agent
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === 'development' && !cert) {
|
||||
return new http.Agent()
|
||||
}
|
||||
|
||||
// we only support HTTPS over clearnet
|
||||
return new https.Agent(httpsAgentOptions)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { msatsToSats } from '@/lib/format'
|
||||
import { getAgent } from '@/lib/proxy'
|
||||
|
||||
export * from 'wallets/lnbits'
|
||||
|
||||
|
@ -27,7 +28,15 @@ export async function createInvoice (
|
|||
out: false
|
||||
})
|
||||
|
||||
const res = await fetch(url + path, { method: 'POST', headers, body })
|
||||
const hostname = url.replace(/^https?:\/\//, '')
|
||||
const agent = getAgent({ hostname })
|
||||
|
||||
const res = await fetch(`${agent.protocol}//${hostname}${path}`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
agent,
|
||||
body
|
||||
})
|
||||
if (!res.ok) {
|
||||
const errBody = await res.json()
|
||||
throw new Error(errBody.detail)
|
||||
|
|
Loading…
Reference in New Issue