getTraefikConfig.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { Request, Response } from "express";
  2. import db from "@server/db";
  3. import { and, eq } from "drizzle-orm";
  4. import logger from "@server/logger";
  5. import HttpCode from "@server/types/HttpCode";
  6. import config from "@server/lib/config";
  7. import { orgs, resources, sites, Target, targets } from "@server/db/schema";
  8. import { sql } from "drizzle-orm";
  9. export async function traefikConfigProvider(
  10. _: Request,
  11. res: Response
  12. ): Promise<any> {
  13. try {
  14. const allResources = await db
  15. .select({
  16. // Resource fields
  17. resourceId: resources.resourceId,
  18. subdomain: resources.subdomain,
  19. fullDomain: resources.fullDomain,
  20. ssl: resources.ssl,
  21. blockAccess: resources.blockAccess,
  22. sso: resources.sso,
  23. emailWhitelistEnabled: resources.emailWhitelistEnabled,
  24. http: resources.http,
  25. proxyPort: resources.proxyPort,
  26. protocol: resources.protocol,
  27. isBaseDomain: resources.isBaseDomain,
  28. // Site fields
  29. site: {
  30. siteId: sites.siteId,
  31. type: sites.type,
  32. subnet: sites.subnet
  33. },
  34. // Org fields
  35. org: {
  36. orgId: orgs.orgId,
  37. domain: orgs.domain
  38. },
  39. // Targets as a subquery
  40. targets: sql<string>`json_group_array(json_object(
  41. 'targetId', ${targets.targetId},
  42. 'ip', ${targets.ip},
  43. 'method', ${targets.method},
  44. 'port', ${targets.port},
  45. 'internalPort', ${targets.internalPort},
  46. 'enabled', ${targets.enabled}
  47. ))`.as("targets")
  48. })
  49. .from(resources)
  50. .innerJoin(sites, eq(sites.siteId, resources.siteId))
  51. .innerJoin(orgs, eq(resources.orgId, orgs.orgId))
  52. .leftJoin(
  53. targets,
  54. and(
  55. eq(targets.resourceId, resources.resourceId),
  56. eq(targets.enabled, true)
  57. )
  58. )
  59. .groupBy(resources.resourceId);
  60. if (!allResources.length) {
  61. return res.status(HttpCode.OK).json({});
  62. }
  63. const badgerMiddlewareName = "badger";
  64. const redirectHttpsMiddlewareName = "redirect-to-https";
  65. const config_output: any = {
  66. http: {
  67. middlewares: {
  68. [badgerMiddlewareName]: {
  69. plugin: {
  70. [badgerMiddlewareName]: {
  71. apiBaseUrl: new URL(
  72. "/api/v1",
  73. `http://${config.getRawConfig().server.internal_hostname}:${
  74. config.getRawConfig().server
  75. .internal_port
  76. }`
  77. ).href,
  78. userSessionCookieName:
  79. config.getRawConfig().server
  80. .session_cookie_name,
  81. accessTokenQueryParam:
  82. config.getRawConfig().server
  83. .resource_access_token_param,
  84. resourceSessionRequestParam:
  85. config.getRawConfig().server
  86. .resource_session_request_param
  87. }
  88. }
  89. },
  90. [redirectHttpsMiddlewareName]: {
  91. redirectScheme: {
  92. scheme: "https"
  93. }
  94. }
  95. }
  96. }
  97. };
  98. for (const resource of allResources) {
  99. const targets = JSON.parse(resource.targets);
  100. const site = resource.site;
  101. const org = resource.org;
  102. if (!org.domain) {
  103. continue;
  104. }
  105. const routerName = `${resource.resourceId}-router`;
  106. const serviceName = `${resource.resourceId}-service`;
  107. const fullDomain = `${resource.fullDomain}`;
  108. if (resource.http) {
  109. // HTTP configuration remains the same
  110. if (!resource.subdomain && !resource.isBaseDomain) {
  111. continue;
  112. }
  113. // add routers and services empty objects if they don't exist
  114. if (!config_output.http.routers) {
  115. config_output.http.routers = {};
  116. }
  117. if (!config_output.http.services) {
  118. config_output.http.services = {};
  119. }
  120. const domainParts = fullDomain.split(".");
  121. let wildCard;
  122. if (domainParts.length <= 2) {
  123. wildCard = `*.${domainParts.join(".")}`;
  124. } else {
  125. wildCard = `*.${domainParts.slice(1).join(".")}`;
  126. }
  127. const tls = {
  128. certResolver: config.getRawConfig().traefik.cert_resolver,
  129. ...(config.getRawConfig().traefik.prefer_wildcard_cert
  130. ? {
  131. domains: [
  132. {
  133. main: wildCard
  134. }
  135. ]
  136. }
  137. : {})
  138. };
  139. logger.debug(config.getRawConfig().traefik.prefer_wildcard_cert)
  140. const additionalMiddlewares =
  141. config.getRawConfig().traefik.additional_middlewares || [];
  142. config_output.http.routers![routerName] = {
  143. entryPoints: [
  144. resource.ssl
  145. ? config.getRawConfig().traefik.https_entrypoint
  146. : config.getRawConfig().traefik.http_entrypoint
  147. ],
  148. middlewares: [
  149. badgerMiddlewareName,
  150. ...additionalMiddlewares
  151. ],
  152. service: serviceName,
  153. rule: `Host(\`${fullDomain}\`)`,
  154. ...(resource.ssl ? { tls } : {})
  155. };
  156. if (resource.ssl) {
  157. config_output.http.routers![routerName + "-redirect"] = {
  158. entryPoints: [
  159. config.getRawConfig().traefik.http_entrypoint
  160. ],
  161. middlewares: [redirectHttpsMiddlewareName],
  162. service: serviceName,
  163. rule: `Host(\`${fullDomain}\`)`
  164. };
  165. }
  166. config_output.http.services![serviceName] = {
  167. loadBalancer: {
  168. servers: targets
  169. .filter((target: Target) => {
  170. if (!target.enabled) {
  171. return false;
  172. }
  173. if (
  174. site.type === "local" ||
  175. site.type === "wireguard"
  176. ) {
  177. if (
  178. !target.ip ||
  179. !target.port ||
  180. !target.method
  181. ) {
  182. return false;
  183. }
  184. } else if (site.type === "newt") {
  185. if (
  186. !target.internalPort ||
  187. !target.method
  188. ) {
  189. return false;
  190. }
  191. }
  192. return true;
  193. })
  194. .map((target: Target) => {
  195. if (
  196. site.type === "local" ||
  197. site.type === "wireguard"
  198. ) {
  199. return {
  200. url: `${target.method}://${target.ip}:${target.port}`
  201. };
  202. } else if (site.type === "newt") {
  203. const ip = site.subnet.split("/")[0];
  204. return {
  205. url: `${target.method}://${ip}:${target.internalPort}`
  206. };
  207. }
  208. })
  209. }
  210. };
  211. } else {
  212. // Non-HTTP (TCP/UDP) configuration
  213. const protocol = resource.protocol.toLowerCase();
  214. const port = resource.proxyPort;
  215. if (!port) {
  216. continue;
  217. }
  218. if (!config_output[protocol]) {
  219. config_output[protocol] = {
  220. routers: {},
  221. services: {}
  222. };
  223. }
  224. config_output[protocol].routers[routerName] = {
  225. entryPoints: [`${protocol}-${port}`],
  226. service: serviceName,
  227. ...(protocol === "tcp" ? { rule: "HostSNI(`*`)" } : {})
  228. };
  229. config_output[protocol].services[serviceName] = {
  230. loadBalancer: {
  231. servers: targets
  232. .filter((target: Target) => {
  233. if (!target.enabled) {
  234. return false;
  235. }
  236. if (
  237. site.type === "local" ||
  238. site.type === "wireguard"
  239. ) {
  240. if (!target.ip || !target.port) {
  241. return false;
  242. }
  243. } else if (site.type === "newt") {
  244. if (!target.internalPort) {
  245. return false;
  246. }
  247. }
  248. return true;
  249. })
  250. .map((target: Target) => {
  251. if (
  252. site.type === "local" ||
  253. site.type === "wireguard"
  254. ) {
  255. return {
  256. address: `${target.ip}:${target.port}`
  257. };
  258. } else if (site.type === "newt") {
  259. const ip = site.subnet.split("/")[0];
  260. return {
  261. address: `${ip}:${target.internalPort}`
  262. };
  263. }
  264. })
  265. }
  266. };
  267. }
  268. }
  269. return res.status(HttpCode.OK).json(config_output);
  270. } catch (e) {
  271. logger.error(`Failed to build Traefik config: ${e}`);
  272. return res.status(HttpCode.INTERNAL_SERVER_ERROR).json({
  273. error: "Failed to build Traefik config"
  274. });
  275. }
  276. }