|
| 1 | +import { execShell } from '@/utils/exec-shell'; |
| 2 | +import { isValidMacAddress } from '@/ip/index'; |
| 3 | + |
| 4 | +const LINK_TYPES = <const>[ |
| 5 | + 'amt', |
| 6 | + 'bareudp', |
| 7 | + 'bond', |
| 8 | + 'bond_slave', |
| 9 | + 'bridge', |
| 10 | + 'bridge_slave', |
| 11 | + 'dsa', |
| 12 | + 'dummy', |
| 13 | + 'erspan', |
| 14 | + 'geneve', |
| 15 | + 'gre', |
| 16 | + 'gretap', |
| 17 | + 'gtp', |
| 18 | + 'ifb', |
| 19 | + 'ip6erspan', |
| 20 | + 'ip6gre', |
| 21 | + 'ip6gretap', |
| 22 | + 'ip6tnl', |
| 23 | + 'ipip', |
| 24 | + 'ipoib', |
| 25 | +]; |
| 26 | + |
| 27 | +export type LinkType = (typeof LINK_TYPES)[number]; |
| 28 | + |
| 29 | +export type AddParams = Record<string, string> & { |
| 30 | + type?: LinkType; |
| 31 | + name?: string; |
| 32 | + txqueuelen?: number; |
| 33 | + address?: string; |
| 34 | + broadcast?: string; |
| 35 | + mtu?: number; |
| 36 | + numtxqueues?: number; |
| 37 | + numrxqueues?: number; |
| 38 | + netns?: string; |
| 39 | +}; |
| 40 | + |
| 41 | +export async function add(params: AddParams) { |
| 42 | + const args = [ |
| 43 | + 'ip', |
| 44 | + 'link', |
| 45 | + 'add', |
| 46 | + ...Object.entries(params).map(([key, value]) => `${key.toLowerCase()} ${value}`), |
| 47 | + ]; |
| 48 | + return execShell(args); |
| 49 | +} |
| 50 | + |
| 51 | +export type DelParams = Record<string, string> & { |
| 52 | + name?: string; |
| 53 | + type?: LinkType; |
| 54 | +}; |
| 55 | + |
| 56 | +export async function del(params: DelParams) { |
| 57 | + const args = [ |
| 58 | + 'ip', |
| 59 | + 'link', |
| 60 | + 'delete', |
| 61 | + ...Object.entries(params).map(([key, value]) => `${key.toLowerCase()} ${value}`), |
| 62 | + ]; |
| 63 | + return execShell(args); |
| 64 | +} |
| 65 | + |
| 66 | +export type ListParams = Record<string, string> & { |
| 67 | + name?: string; |
| 68 | + type?: LinkType; |
| 69 | +}; |
| 70 | + |
| 71 | +export type Link = { |
| 72 | + name: string; |
| 73 | + flags: string[]; |
| 74 | + qdisc: string; |
| 75 | + state: string; |
| 76 | + mode: string; |
| 77 | + group: string; |
| 78 | + qlen?: number; |
| 79 | + mtu: number; |
| 80 | + address?: string; |
| 81 | + broadcast?: string; |
| 82 | +}; |
| 83 | + |
| 84 | +export async function list(params: ListParams = {}): Promise<Link[]> { |
| 85 | + const args = [ |
| 86 | + 'ip', |
| 87 | + 'link', |
| 88 | + 'show', |
| 89 | + ...Object.entries(params).map(([key, value]) => `${key.toLowerCase()} ${value}`), |
| 90 | + ]; |
| 91 | + |
| 92 | + const { error, data } = await execShell(args); |
| 93 | + |
| 94 | + if (error) { |
| 95 | + throw error; |
| 96 | + } |
| 97 | + |
| 98 | + const lines = data.output.match(/^\d+:(.*|(?:\n\s)+)+/gm)!; |
| 99 | + const links: Link[] = []; |
| 100 | + |
| 101 | + for (const line of lines) { |
| 102 | + const link = parseLink(line); |
| 103 | + if (link) { |
| 104 | + links.push(link); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return links; |
| 109 | +} |
| 110 | + |
| 111 | +function parseLink(line: string) { |
| 112 | + if (!line) { |
| 113 | + return undefined; |
| 114 | + } |
| 115 | + |
| 116 | + const parts = line.replace(/\n$/, ' ').split(/\s+/).slice(1); |
| 117 | + if (parts.length < 6) { |
| 118 | + return undefined; |
| 119 | + } |
| 120 | + |
| 121 | + const link: Link = { |
| 122 | + name: parts[0].slice(0, -1), |
| 123 | + flags: parts[1].slice(1, -1).split(','), |
| 124 | + qdisc: '', |
| 125 | + state: '', |
| 126 | + mode: '', |
| 127 | + group: '', |
| 128 | + qlen: undefined, |
| 129 | + mtu: -1, |
| 130 | + address: undefined, |
| 131 | + broadcast: undefined, |
| 132 | + }; |
| 133 | + |
| 134 | + for (const part of parts.slice(2)) { |
| 135 | + const next = parts[parts.indexOf(part) + 1]; |
| 136 | + if (part in link) { |
| 137 | + // @ts-expect-error Type 'string' is not assignable to type 'Link[keyof Link]'. |
| 138 | + link[part] = next.match(/^\d+$/) ? +next : next; |
| 139 | + } |
| 140 | + |
| 141 | + if (part.startsWith('link/') && isValidMacAddress(next)) { |
| 142 | + link.address = next; |
| 143 | + } |
| 144 | + |
| 145 | + if (part === 'brd') { |
| 146 | + link.broadcast = next; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return link; |
| 151 | +} |
0 commit comments