summaryrefslogtreecommitdiff
path: root/Omni/Cloud/Comms/Xmpp.nix
blob: ad8649b6f82916cf7e1bbd372ce0f8984d2a1801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{ config, pkgs, ... }:
#
# xmpp chat service
#
let
  rootDomain = config.networking.domain; # simatime.com
  ssl = {
    cert = "/var/lib/acme/${rootDomain}/fullchain.pem";
    key = "/var/lib/acme/${rootDomain}/key.pem";
  };
in {
  networking.firewall.allowedTCPPorts = [
    # https://prosody.im/doc/ports
    5000 # file transfer
    5222 # client connections
    5269 # server-to-server
    5280 # http
    5281 # https
    5347 # external components
    5582 # telnet console
  ];

  services.prosody = {
    enable = true;
    package =
      pkgs.prosody.override { withCommunityModules = [ "conversejs" ]; };

    # when i learn how to use security.acme better, and use separate certs, then i
    # can fix this group
    group = "nginx";
    admins = [ "bsima@${rootDomain}" ];
    allowRegistration = true;
    inherit ssl;
    uploadHttp = {
      domain = "upload.${rootDomain}";
      uploadExpireAfter = toString (60 * 60 * 24 * 30); # 30 days, as seconds
    };

    modules = {
      announce = true;
      blocklist = true;
      bookmarks = true;
      bosh = true;
      carbons = true;
      cloud_notify = true;
      csi = true;
      dialback = true;
      disco = true;
      groups = true;
      http_files = false; # hm, look into this
      motd = true;
      pep = true;
      ping = true;
      private = true;
      proxy65 = true;
      register = true;
      roster = true;
      server_contact_info = true;
      smacks = true;
      vcard = true;
      watchregistrations = true;
      websocket = true;
      welcome = true;
    };

    extraConfig = ''
      conversejs_options = {
        allow_registration = true;
        bosh_service_url = "https://${rootDomain}/http-bind";
        debug = true;
        loglevel = "debug";
        -- default_domain = "${rootDomain}";
        -- domain_placeholder = "${rootDomain}";
        -- jid = "${rootDomain}";
        -- keepalive = true;
        -- registration_domain = "${rootDomain}";
        websocket_url = "wss://${rootDomain}/xmpp-websocket";
      }

      cross_domain_websocket = { "https://${rootDomain}", "https://anon.${rootDomain}" }
      cross_domain_bosh = false; -- handle this with nginx
      consider_bosh_secure = true;

      -- this is a virtualhost that allows anonymous authentication. use this
      -- for a public lobby. the nix module doesn't support 'authentication'
      -- so i have to do this here.
      VirtualHost "anon.${rootDomain}"
        authentication = "anonymous"
        ssl = {
          cafile = "/etc/ssl/certs/ca-bundle.crt";
          key = "${ssl.key}";
          certificate = "${ssl.cert}";
        };
    '';

    muc = [
      {
        domain = "conference.${rootDomain}";
        maxHistoryMessages = 10000;
        name = "Chat Rooms";
        restrictRoomCreation = "admin";
        roomDefaultHistoryLength = 20;
        roomDefaultMembersOnly = true;
        roomDefaultModerated = true;
        roomDefaultPublic = false;
      }
      {
        domain = "chat.${rootDomain}";
        maxHistoryMessages = 10000;
        name = "Chat Rooms";
        restrictRoomCreation = false;
        roomDefaultHistoryLength = 200;
        roomDefaultMembersOnly = false;
        roomDefaultModerated = false;
        roomDefaultPublic = true;
        roomDefaultPublicJids = true;
      }
    ];

    virtualHosts = {
      "${rootDomain}" = {
        domain = "${rootDomain}";
        enabled = true;
        inherit ssl;
      };
    };
  };

  services.prosody-filer = { enable = true; };

  services.nginx.virtualHosts."${rootDomain}".locations = {
    "/http-bind" = {
      proxyPass = "https://${rootDomain}:5281/http-bind";
      extraConfig = ''
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        add_header Access-Control-Allow-Origin "*";
      '';
    };

    "/xmpp-websocket" = {
      proxyPass = "https://${rootDomain}:5281/xmpp-websocket";
      extraConfig = ''
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
        add_header Access-Control-Allow-Origin "*";
      '';
    };

    "/chat" = {
      proxyPass = "https://${rootDomain}:5281/conversejs";
      extraConfig = ''
        add_header Access-Control-Allow-Origin "*";
      '';
    };
  };

  services.nginx.virtualHosts."anon.${rootDomain}" = {
    useACMEHost = "${rootDomain}";
    forceSSL = true;
    locations = {
      "/http-bind" = {
        proxyPass = "https://anon.${rootDomain}:5281/http-bind";
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_buffering off;
          if ($request_method ~* "(GET|POST)") {
            add_header Access-Control-Allow-Origin "*";
          }
          if ($request_method = OPTIONS) {
            add_header Access-Control-Allow-Origin  "*";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, HEAD";
            add_header Access-Control-Allow-Headers "Authorization, Origin, X-Requested-With, Content-Type, Accept";
            return 200;
          }
        '';
      };
    };
  };

  users.users.nginx.extraGroups = [ "prosody" ];

  security.acme.certs.${rootDomain}.extraDomainNames = [
    # these stopped working idk why
    #"upload.${rootDomain}"
    #"conference.${rootDomain}"
    "anon.${rootDomain}"
    "chat.${rootDomain}"
  ];

  #security.acme.certs.prosody = {
  #  domain = "${domain}";
  #  group = "prosody";
  #  dnsProvider = "rfc2136";
  #  #credentialsFile = config.secrets.files.dns_creds.path;
  #  postRun = "systemctl restart prosody";
  #  extraDomainNames = [
  #    domain
  #    "upload.${domain}"
  #  ];
  #};
}