Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: some transport and authentication docs #829

Merged
merged 6 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Mirage has many new features
* Server Rpcs can [return values](https://miragenet.github.io/Mirage/Articles/Guides/Communications/RemoteActions.html)
* The default transport features DoS prevention
* The default transport has CRC64 integrity check
* Mirage provides unreliable channel out of the box

If you look under the hood, the code base has some significant differences based on the core values of each project
* Mirage follows the [SOLID principles](https://en.wikipedia.org/wiki/SOLID).
Expand All @@ -93,22 +92,14 @@ If you want to contribute to Mirage, follow these steps:
If you don't enable symlinks, you will be able to work on Mirage but Unity will not see the examples.
3) Open in unity 2019.4.x or later

## Transports
Here is a list of some transports supported by NG and how they compare to each other

| | Kcp | [Websocket](https://github.com/MirageNet/WebsocketNG) | [Steam](https://github.com/dragonslaya84/FizzySteamyMirror) | [LiteNetLibNG](https://github.com/uweenukr/LiteNetLibNG) | [IgnoranceNG](https://github.com/dragonslaya84/IgnoranceNG) |
| -------------- | :----------------: | :--------------------------------------------------: | :---------------------------------------------------------: | :------------------------------------------------------: | :---------------------------------------------------------: |
| **CCU** | 1000+ | ? | ? | ? | ? |
| **Protocol** | UDP | TCP | UDP | UDP | UDP |
| **Unreliable** | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| **WebGL** | | :white_check_mark: | | | |
| **Mobile** | :white_check_mark: | | | ? | ? |
| **CPU** | LOW | HIGH | ? | ? | ? |
| **NAT Punch** | | | :white_check_mark: | | |
| **Encryption** | | :white_check_mark: | :white_check_mark: | | |
| **IPV6** | :white_check_mark: | :white_check_mark: | ? | ? | ? |
| **Managed** | :white_check_mark: | :white_check_mark: | | :white_check_mark: | |
| **Based on** | KCP | Websockets | Steam Game Networking Sockets | LiteNetLib | ENet |
## Transport and Sockets

Mirage supports multiple ways of transporting data:
- c# UDP Socket (default)
- Native UDP socket (coming soon)
- Websocket, to support webgl clients (coming soon)
- Steam (coming soon)


## Contributing

Expand Down
75 changes: 38 additions & 37 deletions doc/Articles/Components/Authenticators/index.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
# Authentication

When you have a multiplayer game, often you need to store information about your player for later games, keep game stats or communicate with your friends. For all these use cases, you often need a way to uniquely identify a user. Being able to tell users apart is called authentication. There are several methods available, some examples include:
- Ask the user for username and password
- Use a third party OAuth2 or OpenID identity provider, such as Facebook, Twitter, Google
- Use a third party service such as PlayFab, GameLift or Steam
- Use the device id, very popular method in mobile
- Use Google Play in Android
- Use Game Center in IOS
- Use a web service in your website
For some games you may want to limit who can join or uniquely identity a user in order to save stats or communicate with friends. Authentication is checking if a user is valid and is who they say they are. There are several methods available, some examples include:
- Ask the user for username and password
- Use a third party OAuth2 or OpenID identity provider, such as Facebook, Twitter, Google
- Use a third party service such as PlayFab, GameLift or Steam
- Use the device id, very popular method in mobile
- Use Google Play in Android
- Use Game Center in IOS
- Use a web service in your website


## Encryption Notice

By default Mirage uses Kcp, which is not encrypted, so if you want to do authentication through Mirage, we highly recommend you use a transport that supports encryption.
By default Mirage is not encrypted, so if you want to do authentication through Mirage, we highly recommend you use a transport that supports encryption.

## Basic Authenticator

- [Basic Authenticator](Basic.md)
Mirage includes a Basic Authenticator in the Mirage / Authenticators folder which just uses a simple username and password.
[Basic Authenticator](Basic.md)

Mirage includes a Basic Authenticator in the `Mirage/Authenticators` folder which just uses a simple password. This will only allow people with the password to join the server. For example, a password on a hosted game so that only friends can join.


## Custom Authenticators

Authenticators are derived from an `Authenticator` abstract class that allows you to implement any authentication scheme you need.
To Create a custom authenticator implement the `NetworkAuthenticator` abstract class and override the `ServerAuthenticated` and `ClientAuthenticated` methods.

When a client is successfully authenticated, call `base.OnServerAuthenticated.Invoke(conn)` on the server and `base.OnClientAuthenticated.Invoke(conn)` on the client. Mirage is listening for these events to proceed with the connection sequence. Subscribe to OnServerAuthenticated and OnClientAuthenticated events if you wish to perform additional steps after authentication.
After authenticating a player call either `ServerAccept`, `ServerReject`, `ClientAccept` or `ClientReject` depending if running on Server or Client and if you it was successful or not.

## Message Registration
Calling the `Accept` method will cause mirage to invoke the `OnServerAuthenticated` or `OnClientAuthenticated` events. Subscribe to OnServerAuthenticated and OnClientAuthenticated events if you wish to perform additional steps after authentication.

By default all messages registered to `NetworkServer` and `NetworkClient` require authentication unless explicitly indicated otherwise. To register messages to bypass authentication, you need to specify `false` for a bool parameter to the `RegisterMessage` method:
Calling the `Reject` method will cause the player to be disconnected after a short delay.

When Rejecting, It is a good idea to send a message to the client to tell them that authentication failed, for example: "Server password invalid" or "Login failed".

```
NetworkServer.RegisterHandler<AuthenticationRequest>(OnAuthRequestMessage, false);
```

Certain internal messages already have been set to bypass authentication:
## Check if a player is authenticated

- Server
- `ConnectMessage`
- `DisconnectMessage`
- `ErrorMessage`
- `NetworkPingMessage`
- Client
- `ConnectMessage`
- `DisconnectMessage`
- `ErrorMessage`
- `SceneMessage`
- `NetworkPongMessage`
After a player has been accepted `IsAuthenticated` will be set to true. The bool can be used alongside `AuthenticationData` to check if a user is allowed to do certain actions.

### Tips

- Register handlers for messages in `OnStartServer` and `OnStartClient`. They're called from StartServer/StartHost, and StartClient, respectively.
- Send a message to the client if authentication fails, especially if there's some issue they can resolve.
- Call the `Disconnect()` method of the `NetworkConnection` on the server and client when authentication fails. If you want to give the user a few tries to get their credentials right, you certainly can, but Mirage will not do the disconnect for you.
- Remember to put a small delay on the Disconnect call on the server if you send a failure message so that it has a chance to be delivered before the connection is dropped.
- `NetworkConnection` has an `authenticationData` object where you can drop any data you need to persist on the server related to the authentication, such as account id's, tokens, character selection, etc.
## Storing Authentication data

The `NetworkPlayer` object has an `AuthenticationData` property that can be used to store any data related to authentication, such as account id, tokens, or players username.

This property is of type `object` so can be set to any object and can be cast back to that object when you need to read the data.

```cs
if (player.IsAuthenticated)
{
var loginData = (MyLogInData)player.AuthenticationData;
var username = loginData.Username;
// do something with username :)
}
```


Now that you have the foundation of a custom Authenticator component, the rest is up to you. You can exchange any number of custom messages between the server and client as necessary to complete your authentication process before approving the client.

Authentication can also be extended to character selection and customization, just by crafting additional messages and exchanging them with the client before completing the authentication process. This means this process takes place before the client player actually enters the game or changes to the Online scene.

If you write a good authenticator, consider sharing it with other users or donating it to the Mirage project.
If you write a good authenticator, consider sharing it with other users or contributing it to the Mirage project.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Mirage Quick Start Guide

> [!WARNING]
> This guide was written for mirror so some information might be out-of-date for mirage

This guide currently shows you:
- [Basic scene setup](#part-1)
- [Player movement](#part-4)
Expand All @@ -11,7 +15,6 @@ It is best to first make a mini practice game before converting your single play
The Pre-made Mirage examples are great for using as reference, it is recommend to use them regarding connection setup, with ports and firewalls. This can be a huge topic that changes from person to person, and is not covered in this guide, here we will use localHost (multiple games on same PC).



## Part 1

Blank Project, import UniTask and Mirage using Unity package manager.
Expand Down
2 changes: 1 addition & 1 deletion doc/Articles/Guides/DevServer/gcloud/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ This will allow other people to connect to your server using its IP and port
- Source IP ranges: 0.0.0.0/0
- Protocols or ports: Select UDP, and then enter port 7777 into the field provided.

> note UDP and port 7777 is default settings for KCP transport, if you are using a different transport you will need to find out what settings that uses.
> note UDP and port 7777 is default settings for socket, if you are using a different socket you will need to find out what settings that uses.

![Create network rule](./07-create-network-rule.jpg)

Expand Down
2 changes: 0 additions & 2 deletions doc/Articles/Guides/Sync/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,3 @@ public override void DeserializeSyncVars(NetworkReader reader, bool initialState
```

If a <xref:Mirage.NetworkBehaviour> has a base class that also has serialization functions, the base class functions should also be called.

Note that the `UpdateVar` packets created for game object state updates may be aggregated in buffers before being sent to the client, so a single transport layer packet may contain updates for multiple game objects.