aboutsummaryrefslogtreecommitdiff
path: root/pages/wg2nd/index.tsx
blob: 3f38a1250f3adf520b19fde4418e72b40e705e14 (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
// pages/index.tsx
import Head from 'next/head';
import { useEffect, useRef, useState } from 'react';

import Box from '../../components/Box';
import TextArea from '../../components/TextArea';
import Input from '../../components/Input';
import Button from '../../components/Button';
import Markdown from '../../components/Markdown';
import Typography from '../../components/Typography';

import PathCrumbs from "../../components/PathCrumbs";

import Script from 'next/script';

// @ts-ignore
import desc from './desc.md';


/*
 * HOOK THE MODULE FROM THE DOM
 */

interface Wg2ndModule {
  wg2nd_cmdseq: (intfName : string, intfconfig : string) => string;
}

declare global {
  interface Window {
    Wg2ndModule: any;
    wg2ndOnLoad: (module : Wg2ndModule) => void
  }
}

const useModule = <T extends any>(onInitCallback? : (module: T) => void): T | null => {
  const [module, setModule] = useState<T | null>(null);

  function setModuleWithCallback(module : T) {
    console.log('setting module');

    if(onInitCallback !== undefined) {
      onInitCallback(module);
    }

    setModule(module);
  }

  useEffect(() => {

    window.wg2ndOnLoad = function() {
      setModuleWithCallback(window.Wg2ndModule);
    }

    if(module === null && typeof window.Wg2ndModule === "object") {
      setModuleWithCallback(window.Wg2ndModule);
    }

  });


  return module;
};


const DEMO_CONFIG = `[Interface]
PrivateKey = 0OCS+dV5wsDje6qUAEDQzPmTNWOLE9HE8kfGU1wJUE0=
Address = 10.55.127.342/32, ab00:aaaa:aaa:aa02::5:abcd/128
DNS = 10.64.0.1

[Peer]
PublicKey = WBSnuq6Vswxz5G5zz9pUt60ZSA+JfZ1iTXdg0RJGjks=
AllowedIPs = 0.0.0.0/0,::0/0
Endpoint = 128.45.210.64:51821
`;

let rows = 0;
for(const char of DEMO_CONFIG)
  if(char == '\n') rows++;

interface PanelProps {
  children?: React.ReactNode;
  customStyle?: React.CSSProperties;
};

const Panel: React.FC<PanelProps> = ({ children, customStyle }) => (
    <section style={{
      backgroundColor: '#f8f9fa',
      marginTop: '1.5rem',
      padding: '1.5rem',
      ...customStyle
    }}>
      {children}
    </section>
);

const Wg2nd = () => {
  const intfNameRef = useRef<HTMLInputElement>(null);
  const intfConfigRef = useRef<HTMLTextAreaElement>(null);
  const ndConfigRef = useRef<HTMLTextAreaElement>(null);
  const parentDebounceDiv = useRef<HTMLDivElement>(null);

  const convertConfig = (module : Wg2ndModule) => {
    if(intfNameRef.current === null || intfConfigRef.current === null
       || ndConfigRef.current === null || parentDebounceDiv.current === null) {
      return;
    }

    const intfName = intfNameRef.current!.value;
    const intfConfig = intfConfigRef.current!.value;

    const result = module.wg2nd_cmdseq(intfName, intfConfig);

    // This is a hack: adjust a wrapper around the output text area
    // Thus, we can detect the scrollHeight of the TextArea without
    // the screen moving.
    parentDebounceDiv.current!.style.height = ndConfigRef.current!.style.height;
    ndConfigRef.current!.style.height = `0px`;

    ndConfigRef.current!.value = result;

    const scrollHeight = ndConfigRef.current!.scrollHeight;
    ndConfigRef.current!.style.height = `${scrollHeight}px`;
    parentDebounceDiv.current!.style.height = 'auto';
  };

  const module = useModule<Wg2ndModule>(convertConfig);

  return (
  <>
    <script src="/wg2nd/wg2nd_bindings.js" />
    <main
        style={{
            maxWidth: '1200px',
            margin: 'auto',
        }}
    >
      {/* Description */}
      <Panel>
        <PathCrumbs path="/wg2nd" />
        <Markdown md={desc} />
      </Panel>

      {/* Input Panel */}
      <Panel>
        <Typography variant="h3">WireGuard Configuration</Typography>
        <Box my={1}>
          <label htmlFor="wg_config_intf_name">
            <Typography variant="h4" gutter>Interface Name</Typography>
          </label>
          <Input
            ref={intfNameRef}
            id="wg_config_intf_name"
            defaultValue="wg0"
            spellCheck={false}
          />
        </Box>
        <Box my={1}>
          <label htmlFor="wg_config_conf">
            <Typography variant="h4" gutter>Interface Configuration</Typography>
          </label>
          <TextArea
            ref={intfConfigRef}
            id="wg_config_conf"
            defaultValue={DEMO_CONFIG}
            rows={rows}
            spellCheck={false}
          />
        </Box>
        <Button
          sx={{width: '100%'}}
          onClick={(e) => {
            if(module !== null) {
              convertConfig(module);
            }
          }}
        >Generate</Button>
      </Panel>

      {/* Output Panel */}
      <Panel>
        <label htmlFor="nd_config_cmds">
          <Typography variant="h3">Networkd Configuration</Typography>
        </label>
        <Box my={1}>
          <div ref={parentDebounceDiv} >
            <TextArea
              sx={{ overflowY: 'hidden' }}
              id="nd_config_cmds"
              ref={ndConfigRef}
              spellCheck={false}
              readOnly
            />
          </div>
        </Box>
      </Panel>
    </main>
  </>
  );
};

export default Wg2nd;