# Networking

{% hint style="danger" %}
By intercepting http requests a **majority of DRMs will stop working** for security reasons. If addons like VCMod doesn't work any more, try disabling http.Fetch and http.Post inspection.
{% endhint %}

## Hooks

<details>

<summary>swatch_networking_playerauthenticated</summary>

Called after player spawned and responded to the sWatch authentication.

After this you can use [#swatch.sendlua](#swatch.sendlua "mention").

#### Arguments

1. **(Player)** player

</details>

<details>

<summary>swatch_networking_concommand_run</summary>

Called whenever a command is executed on the server. This only works with commands added with `concommand.Add`.

#### Arguments

1. **(Player)** ply - Can be eighter player or `NULL` for console.
2. **(String)** command
3. **(Any)** arguments
4. **(String)** argumentString

#### Returns

1. **(Boolean)** block - `false` to block the command from running

</details>

<details>

<summary>swatch_networking_runconsolecommand</summary>

Called when `RunConsoleCommand` is executed serverside.

**Arguments**

1. **(String)** command
2. **(Vararg)** arguments

**Returns**

1. **(Boolean)** block - `false` to block the command from running

</details>

<details>

<summary>swatch_networking_post</summary>

Called when `http.Post` is called.

#### Arguments

1. **(String)** domain
2. **(String)** url
3. **(Any)** parameters

#### Returns

1. **(Boolean)** block - `false` to block request

</details>

<details>

<summary>swatch_networking_fetch</summary>

Analogue to [#swatch\_networking\_post](#swatch_networking_post "mention")

</details>

<details>

<summary>swatch_networking_incoming</summary>

Called before `net.Receive`. Used to block or log netmessages received by clients.

#### Arguments

1. **(Player)** player
2. **(String)** netmessageName
3. **(Number)** length

#### Returns

1. **(Boolean)** block - `false` to block message from being received in `net.Receive`

</details>

<details>

<summary>swatch_networking_incoming_post</summary>

Called after net.Receive has been executed. Used to log timing.

#### Arguments

1. **(Player)** player
2. **(String)** netmessageName
3. **(Number)** deltaTime - time in ms it took to execute corresponding `net.Recieve`

</details>

## Functions

<details>

<summary>sWatch.setQuarantine</summary>

All netmessages (except from sWatch) of a player in quarantine will get ignored.

#### Arguments

1. **(Player|SteamID32|SteamID64)** player
2. **(Boolean)** shouldQuarantine - `false` to remove from quarantine, `true` to quarantine

</details>

<details>

<summary>sWatch.isQuarantined</summary>

Returns if a player is quarantined.

#### Arguments

1. **(Player|SteamID32|SteamID64)** player

#### Returns

1. **(Boolean)** isQuarantined

</details>

<details>

<summary>sWatch.queryIPScore</summary>

#### Arguments

1. **(String)** IP-Address
2. **(Player)** player (optional)
3. **(Function)** callback

#### Example

```lua
local ip = ply:IPAddress()
// Extract IP-Address (remove port and block local addresses)
ip = sWatch.extractIP(ip, true)
if not ip then return end
sWatch.queryIPScore(ip, ply, function(data)
    if not data or not data.success then return end
    // your code here
end)
```

You have to first specify an API-Key in the ingame settings. Read more about the callback return value in the official API-Documentation: <https://www.ipqualityscore.com/documentation/proxy-detection/overview>

</details>

<details>

<summary>sWatch.isPlayerAuthenticated</summary>

A player is marked authenticated as soon as we receive our first response netmessage. This may took some time and happens after the `PlayerInitialSpawn` hook is called.

This can be used together with [#swatch.sendlua](#swatch.sendlua "mention") (see a example in section below).

#### Arguments

1. **(Player|SteamID|SteamID64)** player

#### Returns

1. **(Boolean)** isAuthenticated

</details>

<details>

<summary>sWatch.sendLua</summary>

This addon is fileless for the client. So no code is visible via a conventional filestealer. Every code gets send over netmessages and is then run via `RunString` on the client. If `protected` is set true, the code is compiled, compressed and encrypted to prevent manipulation and no `RunString` is used.

#### Arguments

1. **(Player|SteamID32|SteamID64)** player
2. **(String)** code
3. **(Boolean)** protected (optional, default=false) - protection against code manipulation
4. **(Boolean)** cache (optional, default=false) - only necessary if protected=true. Makes protection faster for large payloads.

#### Example

```
// we can only send Lua to authenticated players
if sWatch.isPlayerAuthenticated(ply) then
    sWatch.sendLua(ply, [[
        for i = 1, 10 do
            print(i)
        end
        // you can write arbitrary Lua code here
    ]])
end
```

```
// wait until the player has authenticated
hook.Add("swatch_networking_playerauthenticated", "my_hook", function(ply) 
    sWatch.sendLua(ply, [[
        // you can write arbitrary Lua code here
    ]])
end)
```

</details>

<details>

<summary>sWatch.takeScreenshot</summary>

This is usually called via admin menu. If you want to call it manually on the server, the screenshot will get saved in the data folder. See the ingame options under 'Networking' and 'Screenshot' for more details. Make sure to enable this feature in your settings.

#### Arguments

1. **(Player)** player
2. **(Player)** creator - `nil` if not called via admin menu)

</details>

<details>

<summary>sWatch.netmessage</summary>

This functions creates a netmessage. The real message name gets randomized.

We can restrict the netmessage to our groups ("staff", "protected). If we recieve a message by player that is not allowed to, the message will get blocked and he will get (depending on your settings) punished.&#x20;

Only use this when necessary, as you have to send the ransomized message name to the client. It is idealy for admin menu and anticheat payload.

#### Arguments

1. **(String)** messageName
2. **(String)** restrictedTo - ("staff", "protected" or leave empty for no restrictions)

#### Returns

1. **(String)** randomizedMessageName

#### Example

```
// create new netmessage as soon as the sWatch finish loading
hook.Add("swatch_init_loaded", "my_hook", function()
    net.Receive(sWatch.netmessage("anticheat_detection"), function(len, ply)
        print("received netmessage")
    end)
end)

// send client request to send this netmessage
hook.Add("swatch_networking_playerauthenticated", "my_hook", function(ply) 
    sWatch.sendLua(ply, string.format([[
       net.Start(%q)
       net.SendToServer()
    ]], sWatch.netmessage("my_new_netmessage")))
end)
```

```
// create a netmessage that only staff members are allowed to send
net.Receive(sWatch.netmessage("staff_only_message", "staff"), function() 
end)
// only by protected players
net.Receive(sWatch.netmessage("protected_only_message", "protected"), function() 
end)
// by everyone
net.Receive(sWatch.netmessage("message"), function() 
end)
```

</details>

<details>

<summary>sWatch.getNetmessage</summary>

Returns the original name of a netmessage

#### Arguments

1. **(String)** randomizedMessage

#### Returns

1. **(String)** originalMessageName (first argument of [#swatch.netmessage](#swatch.netmessage "mention"))

</details>

<details>

<summary>sWatch.isInternalNetmessage</summary>

Returns true if the message was created with [#swatch.netmessage](#swatch.netmessage "mention").

#### Arguments

1. **(String)** netmessageName - can be eighter randomized or internal name

#### Returns

1. **(Boolean)** isInternal

</details>

<details>

<summary>sWatch.getPlayerNetmessages</summary>

Returns a table of all netmessages received by a specific player. All internal and randomized messages of this addon will get translated to their original name.

#### Arguments

1. **(Player|SteamID32|SteamID64)** player

#### Returns

1. **(Table)** messages

</details>

<details>

<summary>sWatch.isFakenet</summary>

If enabled in settings, we create fake backdoors and exploits to trick hackers/cheaters/scriptkids into using them.

#### Arguments

1. **(String)** messagename

#### Returns

1. **(Boolean)** isFakenet

{% code lineNumbers="true" %}

```lua
hook.Add("swatch_networking_incoming", "my_hook", function(client, strName, len)
    local isFakeNet = sWatch.isFakenet(strName)
    if isFakeNet then
        print("Player used a fakenet")
        client:Kick()
    end
end)
```

{% endcode %}

</details>

<details>

<summary>sWatch.getExistingBackdoors</summary>

Returns a list of all real backdoors on your server. NOT FAKE BACKDOORS.

#### Returns

1. **(Table)** backdoors

</details>

<details>

<summary>sWatch.getExistingExploits</summary>

Analogue to [#swatch.getexistingbackdoors](#swatch.getexistingbackdoors "mention"). Returns a list of all exploits on your server.

#### Returns

1. **(Table)** exploits

</details>

<details>

<summary>sWatch.getPlayerCommands</summary>

#### Arguments

1. **(Player|SteamID32|SteamID64)** player

#### Returns

1. **(Table)** concommands

</details>
