This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
336 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.arimil.chataraxia; | ||
|
||
import java.io.Serializable; | ||
import java.net.Socket; | ||
|
||
public abstract class Message implements Serializable { | ||
public void process(Socket sender) { | ||
if(Main.isServerSide) { | ||
server(sender); | ||
} else { | ||
client(sender); | ||
} | ||
} | ||
protected abstract void client(Socket sender); | ||
protected abstract void server(Socket sender); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.arimil.chataraxia.client; | ||
|
||
import com.arimil.chataraxia.Message; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
|
||
public class ClientConnection implements Runnable { | ||
|
||
private String host; | ||
private int port; | ||
Socket socket = new Socket(); | ||
private ObjectOutputStream outputStream; | ||
|
||
ClientConnection(String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
} | ||
|
||
public void send(Object obj) { | ||
try { | ||
if(outputStream != null) { | ||
outputStream.writeObject(obj); | ||
outputStream.reset(); | ||
} else { | ||
Client.contoller.addMessage("Connect before trying to send messages '/connect <host> <port>'"); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
socket.connect(new InetSocketAddress(host, port)); | ||
Client.contoller.addMessage("Connected to " + host + " on " + port); | ||
outputStream = new ObjectOutputStream(socket.getOutputStream()); | ||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); | ||
while (socket.isConnected()) { | ||
Object obj = inputStream.readObject(); | ||
Message msg = (Message)obj; | ||
msg.process(socket); | ||
} | ||
} catch (IOException e) { | ||
Client.contoller.addMessage("Unable to connect to " + host + " on " + port); | ||
e.printStackTrace(); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.arimil.chataraxia.messages; | ||
|
||
import com.arimil.chataraxia.Message; | ||
import com.arimil.chataraxia.client.Client; | ||
import com.arimil.chataraxia.server.ClientData; | ||
import com.arimil.chataraxia.server.Server; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.net.Socket; | ||
import java.util.Map; | ||
|
||
public class ChatMessage extends Message { | ||
|
||
private String from; | ||
private String message; | ||
|
||
public ChatMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
protected void client(Socket sender) { | ||
if(from != null) { | ||
Client.contoller.addMessage(from, message); | ||
} else { | ||
Client.contoller.addMessage(message); | ||
} | ||
} | ||
|
||
@Override | ||
protected void server(Socket sender) { | ||
this.from = Server.clients.get(sender).getName(); | ||
for (Map.Entry<Socket, ClientData> entry : Server.clients.entrySet()) | ||
try { | ||
ObjectOutputStream outputStream = entry.getValue().getOutputStream(); | ||
outputStream.writeObject(this); | ||
outputStream.reset(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.arimil.chataraxia.messages; | ||
|
||
import com.arimil.chataraxia.Message; | ||
import com.arimil.chataraxia.server.Server; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.FileSystems; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class LoginMessage extends Message { | ||
|
||
String name; | ||
String pass; | ||
|
||
public LoginMessage(String name, String pass) { | ||
this.name = name; | ||
this.pass = pass; | ||
} | ||
|
||
@Override | ||
protected void client(Socket sender) { | ||
|
||
} | ||
|
||
@Override | ||
protected void server(Socket sender) { | ||
File file = FileSystems.getDefault().getPath("users\\"+name).toFile(); | ||
try { | ||
ObjectOutputStream outputStream = Server.clients.get(sender).getOutputStream(); | ||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
byte[] hash = digest.digest(pass.getBytes(StandardCharsets.UTF_8)); | ||
String passwordHash = new String(hash, StandardCharsets.UTF_8); | ||
if(file.exists()) { | ||
BufferedReader br = new BufferedReader(new FileReader(file)); | ||
if(br.readLine().equals(passwordHash)) { | ||
Server.clients.get(sender).setAuth(true); | ||
} else { | ||
outputStream.writeObject(new ChatMessage("Invalid username or password")); | ||
} | ||
} else { | ||
BufferedWriter bw = new BufferedWriter(new FileWriter(file)); | ||
bw.write(passwordHash); | ||
Server.clients.get(sender).setAuth(true); | ||
outputStream.writeObject(new ChatMessage("Logged in successfully")); | ||
} | ||
outputStream.reset(); | ||
} catch (IOException | NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.arimil.chataraxia.server; | ||
|
||
import java.io.ObjectOutputStream; | ||
|
||
public class ClientData { | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
private String name; | ||
|
||
public boolean isAuth() { | ||
return auth; | ||
} | ||
|
||
public void setAuth(boolean auth) { | ||
this.auth = auth; | ||
} | ||
|
||
private boolean auth = false; | ||
|
||
public ObjectOutputStream getOutputStream() { | ||
return outputStream; | ||
} | ||
|
||
public void setOutputStream(ObjectOutputStream outputStream) { | ||
this.outputStream = outputStream; | ||
} | ||
|
||
private ObjectOutputStream outputStream; | ||
} |
Oops, something went wrong.