~swisschili/chat

38c1d701d5635831c29ae81740c2336711589a16 — swissChili 4 months ago 6d8344f master
Use transport security
M client/src/main/java/sh/swisschili/chat/client/InfiniteScrollPane.java => client/src/main/java/sh/swisschili/chat/client/InfiniteScrollPane.java +2 -1
@@ 31,12 31,13 @@ public class InfiniteScrollPane<T> extends JScrollPane {
    private final JList<T> list;

    private static final Logger LOGGER = LoggerFactory.getLogger(InfiniteScrollPane.class);
    private int numberToShowAtOnce = 20;
    private int numberToShowAtOnce = 50;
    private final AtomicBoolean fetchingMore = new AtomicBoolean(false);

    private BufferedLoader<T> bufferedLoader = null;
    private final ItemAddedListener listener = new ItemAddedListener() {
        private boolean isAtBottom;

        @Override
        public void beforeItemAdded() {
            int min = getVerticalScrollBar().getMinimum();

M client/src/main/java/sh/swisschili/chat/client/MainWindow.java => client/src/main/java/sh/swisschili/chat/client/MainWindow.java +3 -1
@@ 213,7 213,7 @@ public class MainWindow {

        groupModel.addElement(serverGroup);

        updateStatusFromUi();
        //updateStatusFromUi();
    }

    private void groupSelected() {


@@ 230,6 230,8 @@ public class MainWindow {

    private void onError(Throwable t) {
        SwingUtilities.invokeLater(() -> {
            System.err.println("ERROR OCCURRED:");
            t.printStackTrace();
            JOptionPane.showMessageDialog(panel1, t.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        });
    }

M client/src/main/java/sh/swisschili/chat/client/MessageCell.java => client/src/main/java/sh/swisschili/chat/client/MessageCell.java +12 -5
@@ 40,9 40,14 @@ public class MessageCell implements ListCellRenderer<Message> {
    public MessageCell() {
    }

    public MessageCell(@NotNull Message value, int parentWidth) {
    public MessageCell(@NotNull Message value, int parentWidth, boolean showHeader) {
        $$$setupUI$$$();

        if (!showHeader) {
            sender.setVisible(false);
            time.setVisible(false);
        }

        body.setText(value.getBody());
        body.setLineWrap(true);
        body.setWrapStyleWord(true);


@@ 54,14 59,17 @@ public class MessageCell implements ListCellRenderer<Message> {
        time.setText(new Date(value.getUnixTime()).toString());
        Font f = sender.getFont();
        sender.setFont(f.deriveFont(f.getStyle() | Font.BOLD));

        rootPanel.setBorder(DarkBorders.createLineBorder(2, 0, 0, 0));
    }

    @Override
    public JComponent getListCellRendererComponent(JList<? extends Message> list, Message value, int index,
                                                   boolean isSelected, boolean cellHasFocus) {
        return new MessageCell(value, list.getWidth()).rootPanel;
        // Hide header if multiple messages from same sender
        boolean showHeader = true;
        if (index > 0) {
            showHeader = !list.getModel().getElementAt(index - 1).getSender().equals(value.getSender());
        }
        return new MessageCell(value, list.getWidth(), showHeader).rootPanel;
    }

    /**


@@ 119,5 127,4 @@ public class MessageCell implements ListCellRenderer<Message> {
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }

}

M client/src/main/java/sh/swisschili/chat/client/ServerGroup.java => client/src/main/java/sh/swisschili/chat/client/ServerGroup.java +3 -1
@@ 44,6 44,7 @@ public class ServerGroup {

    private List<ServerChannel> channels = null;
    private User authorizedUser;
    private User user;

    private static final Logger LOGGER = LoggerFactory.getLogger(ServerGroup.class);



@@ 62,6 63,7 @@ public class ServerGroup {
        this.error = error;
        this.listener = listener;
        this.groupName = groupName;
        this.user = user;

        ServerGroup serverGroup = this;



@@ 151,7 153,7 @@ public class ServerGroup {

    public void setStatus(UserStatus status) {
        status = UserStatus.newBuilder(status)
                .setUser(authorizedUser)
                .setUser(user)
                .build();

        LOGGER.info(String.format("Setting status: %s\n", status.toString()));

M server/src/main/java/sh/swisschili/chat/server/ChatServer.java => server/src/main/java/sh/swisschili/chat/server/ChatServer.java +23 -8
@@ 26,6 26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sh.swisschili.chat.util.Constants;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeoutException;



@@ 34,15 35,20 @@ public class ChatServer {
    private final Server server;
    private static final Logger LOGGER = LoggerFactory.getLogger(ChatServer.class.getName());

    public ChatServer(String host, String mqHost, int mqPort, int port, ServerDatabase db, boolean unsafe) throws IOException, TimeoutException {
        this.port = port;
        ChatService chatService = new ChatService(mqHost, mqPort, db);
        chatService.setAllowUnsignedMessages(unsafe);
        server = ServerBuilder
    public ChatServer(ServerDatabase db, Args args) throws IOException, TimeoutException {
        this.port = args.port;
        ChatService chatService = new ChatService(args.mqHost, args.mqPort, db);
        chatService.setAllowUnsignedMessages(args.unsafe);
        ServerBuilder<?> builder = ServerBuilder
                .forPort(port)
                .addService(chatService)
                .addService(new AuthService(db, host))
                .build();
                .addService(new AuthService(db, args.host));

        if (args.ssl) {
            builder = builder.useTransportSecurity(new File(args.certificate), new File(args.privateKey));
        }

        server = builder.build();
    }

    public void start() throws IOException {


@@ 68,6 74,15 @@ public class ChatServer {

        @Parameter(names = { "--unsafe", "-u" }, description = "Forego signature validation (DO NOT use in production)")
        private Boolean unsafe = false;

        @Parameter(names = { "--ssl", "-s" }, description = "Use SSL")
        private Boolean ssl = false;

        @Parameter(names = { "--cert" }, description = "SSL Certificate")
        private String certificate = null;

        @Parameter(names = { "--private-key" }, description = "SSL Private key")
        private String privateKey = null;
    }

    public static void main(String[] argv) throws IOException, TimeoutException {


@@ 82,7 97,7 @@ public class ChatServer {
        LOGGER.info("Launching server");

        ServerDatabase db = new ServerDatabase(args.mongoUrl);
        ChatServer server = new ChatServer(args.host, args.mqHost, args.mqPort, args.port, db, args.unsafe);
        ChatServer server = new ChatServer(db, args);
        server.start();
    }
}

M util/src/main/java/sh/swisschili/chat/util/Constants.java => util/src/main/java/sh/swisschili/chat/util/Constants.java +1 -1
@@ 5,7 5,7 @@ public class Constants {

    public static String LICENSE_HEADER =
            "Decentralized chat software\n" +
            "Copyright (C) 2020  swissChili\n\n" +
            "Copyright (C) 2021  swissChili\n\n" +

            "This program is free software: you can redistribute it and/or modify\n" +
            "it under the terms of the GNU Affero General Public License as published by\n" +

M util/src/main/java/sh/swisschili/chat/util/ServerPool.java => util/src/main/java/sh/swisschili/chat/util/ServerPool.java +12 -3
@@ 33,11 33,20 @@ import java.util.HashMap;
public class ServerPool {
    private final HashMap<String, Channel> channels = new HashMap<>();

    private static final boolean useSsl = System.getenv("CHAT_NO_SSL") == null;

    private Channel channelFor(String server) {
        if (!channels.containsKey(server)) {
            Channel channel = ManagedChannelBuilder.forAddress(server.trim(), Constants.DEFAULT_SERVER_PORT)
                    .usePlaintext()
                    .build();
            ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(server.trim(), Constants.DEFAULT_SERVER_PORT);

            if (useSsl) {
                builder = builder.useTransportSecurity();
            } else {
                builder = builder.usePlaintext();
            }

            Channel channel = builder.build();

            channels.put(server, channel);
            return channel;
        }